All Projects → chillerlan → php-traits

chillerlan / php-traits

Licence: MIT License
A collection of (more or less) useful traits for PHP7.2+

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-traits

witchcraft
Opionated PHP magic methods as traits for PHP 5.4+
Stars: ✭ 23 (+35.29%)
Mutual labels:  magic, trait
Has Parameters
A trait that allows you to pass arguments to Laravel middleware in a more PHP'ish way.
Stars: ✭ 149 (+776.47%)
Mutual labels:  helper, trait
express-mongoose-es8-rest-api
A Boilerplate for developing Rest api's in Node.js using express with support for ES6,ES7,ES8 ,Mongoose,JWT for authentication,Standardjs for linting
Stars: ✭ 20 (+17.65%)
Mutual labels:  dotenv
array-diff-multidimensional
Compare the difference between two multidimensional arrays in PHP
Stars: ✭ 60 (+252.94%)
Mutual labels:  array
ra-ra
A C++20 array / expression template library with some J/APL features
Stars: ✭ 22 (+29.41%)
Mutual labels:  array
arraync
Async Array methods polyfills
Stars: ✭ 16 (-5.88%)
Mutual labels:  array
cmake modules
CMake helper files which provide a wide range of functionality
Stars: ✭ 21 (+23.53%)
Mutual labels:  helper
pscale-workflow-helper-scripts
Workflows and helper scripts around the PlanetScale DB workflow to automate database branch creation, association, update and merge directly out of your pull/merge request or favourite CI/CD.
Stars: ✭ 42 (+147.06%)
Mutual labels:  helper
array-validation
Validation utilities for array structure
Stars: ✭ 14 (-17.65%)
Mutual labels:  array
ctxutil
utils for Go context
Stars: ✭ 18 (+5.88%)
Mutual labels:  helper
ts-dotenv
Strongly-typed environment variables for Node.js
Stars: ✭ 18 (+5.88%)
Mutual labels:  dotenv
pipsalabim
An assistant to guess your pip dependencies from your code, without using a requirements file.
Stars: ✭ 15 (-11.76%)
Mutual labels:  magic
teks
Easily get custom go template based outputs to your command-line tool. Like in docker/kubernetes
Stars: ✭ 41 (+141.18%)
Mutual labels:  helper
ShinyTester
An R package to help debug Shiny apps during the process itself.
Stars: ✭ 29 (+70.59%)
Mutual labels:  helper
dotty dict
Dictionary wrapper for quick access to deeply nested keys.
Stars: ✭ 67 (+294.12%)
Mutual labels:  helper
SNAP
Easy data format saving and loading for GameMaker Studio 2.3.2
Stars: ✭ 49 (+188.24%)
Mutual labels:  array
AmniXTension
A Kotlin extensions + Utils library with Bunch of Help
Stars: ✭ 34 (+100%)
Mutual labels:  helper
MCUCapture
Utility for plotting array data from MCU RAM
Stars: ✭ 22 (+29.41%)
Mutual labels:  array
zeldaPlay
A Single Page Application to help zeldaPlay players to track their characters and progress
Stars: ✭ 95 (+458.82%)
Mutual labels:  magic
gconfigs
gConfigs - Config and Secret parser
Stars: ✭ 42 (+147.06%)
Mutual labels:  dotenv

chillerlan/php-traits

A collection of (more or less) useful traits for PHP7.2+

version license Travis Coverage Scrunitizer Packagist downloads PayPal donate

Features

  • ClassLoader - invokes objects of a given class and interface/type with an arbitrary count of constructor arguments
  • Magic - turns methods into magic properties
  • Enumerable - provides some of prototype's enumerable methods, implements EnumerableInterface
  • ArrayHelpers
    • ByteArray - useful for byte/bit-flipping purposes, extends SplFixedArray
    • ByteArrayDispenser - creates ByteArray from several data types (hex, base64, binary, json etc.)
    • DotArray - adds dot key notation functionality
    • SearchableArray - deep search arrays using RecursiveIteratorIterator
  • Interfaces
  • SPL

Documentation

Installation

requires composer

composer.json (note: replace dev-master with a version boundary)

{
	"require": {
		"php": "^7.2",
		"chillerlan/php-traits": "dev-master"
	}
}

Manual installation

Download the desired version of the package from master or release and extract the contents to your project folder. After that:

  • run composer install to install the required dependencies and generate /vendor/autoload.php.
  • if you use a custom autoloader, point the namespace chillerlan\Traits to the folder src of the package

Profit!

Usage

ClassLoader

Simple usage:

class MyClass{
	use ClassLoader;
	
	protected function doStuff(string $class){
		$obj = $this->loadClass(__NAMESPACE__.'\\Whatever\\'.$class);
		
		// do stuff
	}
}

Let's assume we have several classes that implement the same interface, but their constructors have different parameter counts, like so:

class SomeClass implements MyInterface{
	public funtion __construct($param_foo){}
}

class OtherClass implements MyInterface{
	public funtion __construct($param_foo, $param_bar){}
}

Initialize an object based on a selction

class MyClass{
	use ClassLoader;
	
	protected $classes = [
		'foo' => SomeClass::class, 
		'bar' => OtherClass::class
	];
	
	protected funtion initInterface(string $whatever, $foo, $bar = null):MyInterface{
	
		foreach($this->classes as $what => $class){
			if($whatever === $what){
				return $this->loadClass($class, MyInterface::class, $foo, $bar);
			}
		}
	
	}
}

Magic

Magic allows to access internal methods like as properties.

class MyMagicContainer{
	use Magic;

	protected $foo;

	protected function magic_get_foo(){
		// do whatever...
		
		return 'foo: '.$this->foo;
	}

	protected function magic_set_foo($value){
		// do stuff with $value
		// ...
		
		$this->foo = $value.'bar';
	}
}
$magic = new MyMagicContainer;

$magic->foo = 'foo';

var_dump($magic->foo); // -> foo: foobar

Enumerable

class MyEnumerableContainer implements EnumerableInterface{
	use Enumerable;

	public function __construct(array $data){
		$this->array = $data;
	}
}
$enum = new MyEnumerableContainer($data);

$enum
	->__each(function($value, $index){
		// do stuff
		
		$this->array[$index] = $stuff;
	})
	->__reverse()
	->__to_array()
;

$arr = $enum->__map(function($value, $index){
	// do stuff
	
	return $stuff;
});

$enum;
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].