Prototype in PHP


class Prototype
{
    protected static $prototype_methods = array();
    protected static $prototype_properties = array();
   
    protected $prototype_obj_properties = array();
   
    public static function add_method()
    {
        $args = func_get_args();
        $method = array_shift( $args );
        $callback = str_replace( '$this', '$self', array_pop( $args ) );
        array_push( $args, '$self' );
        $args = implode( ',', $args);
       
        self::$prototype_methods[$method] = create_function( $args, $callback );
    }
   
    public static function add_property( $name )
    {
        self::$prototype_properties[] = $name;
    }
   
    public function __construct()
    {
        if ( self::$prototype_properties ) {
            $this->prototype_obj_properties = array_combine(
                self::$prototype_properties,
                array_fill( 0, count(self::$prototype_properties), null )
                );
        }
    }
   
    public function __call( $method, $args = array() )
    {
        if ( isset( self::$prototype_methods[$method] ) ) {
            array_push( $args, $this );
            return call_user_func_array( self::$prototype_methods[$method], $args );
        }
    }
   
    public function __get( $name )
    {
        if ( in_array( $name, self::$prototype_properties ) ) {
            return isset( $this->prototype_obj_properties[$name] ) ? $this->prototype_obj_properties[$name] : null;
        }
        else {
            return false;
        }
    }
   
    public function __set( $name, $value )
    {
        if ( in_array( $name, self::$prototype_properties ) ) {
            return $this->prototype_obj_properties[$name] = $value;
        }
        else {
            return false;
        }
    }
   
    public function __isset( $name )
    {
        return isset( $this->prototype_obj_properties[$name] );
    }
   
    public function __unset( $name )
    {
        unset( $this->prototype_obj_properties[$name] );
    }
}

class Foo extends Prototype
{
    public $foobar;
   
    function bar()
    {
        echo "bar()\n";
    }
}

$baz = new Foo;
$baz->bar();

Foo::add_method( 'faz', 'echo "faz()\n";' );
$baz->faz();

Foo::add_property( 'taz' );
$baz->taz = "taz\n";
echo $baz->taz;



Foo::add_method( 'raz', 'echo $this->bar(), $this->faz(), $this->taz;' );
$baz->raz();

// taz is not set, so it is null.
$a = new Foo;
$a->raz();
var_dump( $a->taz );


Output:

$baz->bar():
> 'bar()'

$baz->faz():
> 'faz()'

echo $baz->taz:
> 'taz'

$baz->raz():
> 'bar()'
> 'faz()'
> 'taz'

$a->raz():
> 'bar()'
> 'faz()'
>

var_dump( $a->taz ):
> NULL
There are no comments on this page.
Page was generated in 0.4607 seconds