2017 © Pedro Peláez
 

library prototypr

PHP 5.5+ object prototyping system using traits

image

jgswift/prototypr

PHP 5.5+ object prototyping system using traits

  • Monday, June 2, 2014
  • by jgswift
  • Repository
  • 2 Watchers
  • 2 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

prototypr

PHP 5.5+ object prototyping system using traits, (*1)

Build Status Scrutinizer Code Quality, (*2)

Installation

Install via composer:, (*3)

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

The Versions

02/06 2014

dev-master

9999999-dev

PHP 5.5+ object prototyping system using traits

  Sources   Download

MIT

The Requires

 

The Development Requires

php trait magic prototype prototypal