digitalhydra
12/13/2013 - 9:50 PM

php super class

php super class

<?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();