Skip to content

Instantly share code, notes, and snippets.

@bastos
Forked from dhotson/oo.php
Created September 22, 2010 19:54
Show Gist options
  • Select an option

  • Save bastos/592414 to your computer and use it in GitHub Desktop.

Select an option

Save bastos/592414 to your computer and use it in GitHub Desktop.

Revisions

  1. @dhotson dhotson revised this gist Sep 22, 2010. 1 changed file with 57 additions and 36 deletions.
    93 changes: 57 additions & 36 deletions oo.php
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,66 @@
    <?php

    // Define the 'class' class
    $class = Obj()
    ->fn('new', function ($class) {
    $newClass = Obj($class->methods)
    ->fn('new', function($class) {
    $obj = Obj($class->imethods);
    $args = func_get_args();
    array_shift($args);
    call_user_func_array(array($obj, 'init'), $args);
    return $obj;
    });
    return $newClass;
    })
    ->fn('def', function ($t, $name, $fn) {
    $t->imethods[$name] = $fn;
    return $t;
    })
    ->fn('extend', function ($t) {
    return clone $t;
    });

    // Define a new class
    $animal = $class->new()
    ->def('init', function($t, $name) {
    $t->name = $name;
    })
    ->def('speak', function($t) {
    echo "My name is $t->name\n";
    });

    // Extend a class
    $dog = $animal->extend()
    ->def('speak', function($t) {
    echo "My name is $t->name, I have just met you and I love you, SQUIRREL!\n";
    })
    ->def('bark', function($t) {
    echo "Woof!\n";
    });

    $jimmy = $animal->new('Jimmy');
    $jimmy->speak();

    $doug = $dog->new('Doug');
    $doug->speak();
    $doug->bark();


    // ---- The guts...

    class Obj
    {
    public static function _new($methods=array())
    {
    return new self($methods);
    }

    public function __construct($methods=array())
    {
    $this->methods = $methods;
    }

    public function method($name)
    {
    if (!isset($this->methods[$name]))
    throw new BadMethodCallException();

    return $this->methods[$name];
    }

    @@ -32,34 +79,8 @@ public function __call($name, $args)
    }
    }

    // Define the 'class' class
    $class = Obj::_new()
    ->fn('create', function ($class) {
    $newClass = Obj::_new($class->methods)
    ->fn('create', function($class) {
    $obj = new Obj($class->imethods);
    $args = func_get_args();
    array_shift($args); // Strip off first $t argument
    call_user_func_array(array($obj, 'init'), $args);
    return $obj;
    });
    return $newClass;
    })
    ->fn('def', function ($t, $name, $fn) {
    $t->imethods[$name] = $fn;
    return $t;
    })
    ;

    // Define a new class
    $user = $class->create()
    ->def('init', function($t, $name) {
    $t->name = $name;
    })
    ->def('speak', function($t) {
    echo "My name is '$t->name'\n";
    })
    ;

    $doug = $user->create('doug');
    $doug->speak();
    // Allow chaining method calls off the constructor..
    function Obj($methods=array())
    {
    return new Obj($methods);
    }
  2. @dhotson dhotson created this gist Sep 22, 2010.
    65 changes: 65 additions & 0 deletions oo.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    <?php

    class Obj
    {
    public static function _new($methods=array())
    {
    return new self($methods);
    }

    public function __construct($methods=array())
    {
    $this->methods = $methods;
    }

    public function method($name)
    {
    return $this->methods[$name];
    }

    public function fn($name, $fn)
    {
    $this->methods[$name] = $fn;
    return $this;
    }

    public function __call($name, $args)
    {
    return call_user_func_array(
    $this->method($name),
    array_merge(array($this), $args)
    );
    }
    }

    // Define the 'class' class
    $class = Obj::_new()
    ->fn('create', function ($class) {
    $newClass = Obj::_new($class->methods)
    ->fn('create', function($class) {
    $obj = new Obj($class->imethods);
    $args = func_get_args();
    array_shift($args); // Strip off first $t argument
    call_user_func_array(array($obj, 'init'), $args);
    return $obj;
    });
    return $newClass;
    })
    ->fn('def', function ($t, $name, $fn) {
    $t->imethods[$name] = $fn;
    return $t;
    })
    ;

    // Define a new class
    $user = $class->create()
    ->def('init', function($t, $name) {
    $t->name = $name;
    })
    ->def('speak', function($t) {
    echo "My name is '$t->name'\n";
    })
    ;

    $doug = $user->create('doug');
    $doug->speak();