dev-master
9999999-devPHP 5.5+ object prototyping system using traits
MIT
The Requires
- php >=5.5
- jgswift/qtil dev-master
The Development Requires
by Josh Swift
php trait magic prototype prototypal
Wallogit.com
2017 © Pedro Peláez
PHP 5.5+ object prototyping system using traits
PHP 5.5+ object prototyping system using traits, (*1)
Install via composer:, (*3)
php composer.phar require jgswift/prototypr:dev-master
Prototypr is a lightweight php trait that enables easy object prototyping with magic methods. Prototypr aims to add simple prototypal behavior to php without intruding on your domain model, (*4)
The following is a minimal example, (*5)
<?php
class Foo
{
use prototypr\Prototype;
}
Foo::bar(function() {
return "baz";
});
$foo = new Foo;
var_dump($foo->bar()); // returns "baz"
Alternatively methods can be set in a local scope and apply only to an individual object, (*6)
class Foo
{
use prototypr\Prototype;
}
$foo = new Foo();
$foo->bar(function() {
return "baz";
});
var_dump($foo->bar()); // returns "baz"
prototypr supports late-binding of multiple closures and will always execute all closures regardless of return conditions, (*7)
class Foo
{
use prototypr\Prototype;
}
$count = 0;
Foo::bar(function()use(&$count) {
$count+=1;
});
Foo::bar(function()use(&$count) {
$count+=2;
});
$foo = new Foo();
$foo->bar();
var_dump($count); // returns 3
prototypr will automatically traverse the class tree to find methods, but you may also specify individual extensions, (*8)
class Foo
{
use prototypr\Prototype;
}
Foo::bar(function() {
return "somethingImportant";
});
class Baz
{
use prototypr\Prototype;
}
prototypr\Manager::extend('Baz','Foo');
var_dump(count(prototypr\Registry::prototypes('Baz'))); // returns 1
$baz = new Baz;
var_dump($baz->bar()); // returns "somethingImportant"
PHP 5.5+ object prototyping system using traits
MIT
php trait magic prototype prototypal