netmosfera/symbola

Symbola


Keywords
closure, class, functional, methods, callable, first
License
MIT

Documentation

Symbola

Build Status

First class methods & more for PHP 7 (Experimental)

Why use it?

  • Always type-hint for Closure (and never callable, which has strange semantics)
  • No need to hardcode method names as string; [$this, 'method'] will simply be $this->method
  • No need for special syntax for calling "callable fields": ($this->field)() will simply be $this->field()
/**
 * @method string foo()
 * @property Closure $bar
 */
class Something
{
    use Symbola;

    public $foo;

    function __construct(){
        $this->foo = function(){
            return "I'm foo!";
        };
    }

    function bar(){
        return "I'm bar!";
    }
}

$object = new Something;

echo $object->foo(); // Prints "I'm foo!"

$bar = $object->bar;
echo $bar(); // Prints "I'm bar!"

Install through Composer:

composer require netmosfera/symbola