2017 © Pedro Peláez
 

library php-traits

Some useful traits for PHP 7+

image

chillerlan/php-traits

Some useful traits for PHP 7+

  • Friday, June 22, 2018
  • by codemasher
  • Repository
  • 1 Watchers
  • 5 Stars
  • 23,668 Installations
  • PHP
  • 17 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 18 Versions
  • 82 % Grown

The README.md

chillerlan/php-traits

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

version ![license][license-badge] ![Travis][travis-badge] ![Coverage][coverage-badge] ![Scrunitizer][scrutinizer-badge] ![Packagist downloads][downloads-badge] ![PayPal donate][donate-badge], (*2)

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, (*3)

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

{
    "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, (*5)

Profit!, (*6)

Usage

ClassLoader

Simple usage:, (*7)

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:, (*8)

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, (*9)

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., (*10)

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;

The Versions

22/06 2018

dev-master

9999999-dev https://github.com/chillerlan/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

container helper trait dotenv php7

22/06 2018

1.1.13

1.1.13.0 https://github.com/chillerlan/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

container helper trait dotenv php7

22/04 2018

1.1.12

1.1.12.0 https://github.com/chillerlan/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

container helper trait dotenv php7

10/04 2018

1.1.11

1.1.11.0 https://github.com/chillerlan/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

container helper trait dotenv php7

09/04 2018

1.1.10

1.1.10.0 https://github.com/chillerlan/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

container helper trait dotenv php7

28/01 2018

1.1.9

1.1.9.0 https://github.com/chillerlan/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

container helper trait dotenv php7

25/01 2018

1.1.8

1.1.8.0 https://github.com/chillerlan/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

container helper trait dotenv php7

23/01 2018

1.1.7

1.1.7.0 https://github.com/chillerlan/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

container helper trait dotenv php7

22/01 2018

1.1.6

1.1.6.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

21/01 2018

1.1.5

1.1.5.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

13/01 2018

1.1.4

1.1.4.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

09/01 2018

1.1.3

1.1.3.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

03/01 2018

1.1.2

1.1.2.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

30/12 2017

1.1.1

1.1.1.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

11/12 2017

1.1.0

1.1.0.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

26/11 2017

1.0.2

1.0.2.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

25/11 2017

1.0.1

1.0.1.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7

23/11 2017

1.0.0

1.0.0.0 https://github.com/codemasher/php-traits

Some useful traits for PHP 7+

  Sources   Download

MIT

The Requires

  • php >=7.0.3

 

The Development Requires

trait php7