jgswift/prototypr

PHP 5.5+ object prototyping system using traits

dev-master 2014-06-02 13:00 UTC

This package is not auto-updated.

Last update: 2024-05-11 13:05:21 UTC


README

PHP 5.5+ object prototyping system using traits

Build Status Scrutinizer Code Quality

Installation

Install via composer:

php composer.phar require jgswift/prototypr:dev-master

Usage

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

The following is a minimal example

<?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

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

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

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"