Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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 function __construct($methods=array())
{
$this->methods = $methods;
}
public function method($name)
{
if (!isset($this->methods[$name]))
throw new BadMethodCallException();
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)
);
}
}
// Allow chaining method calls off the constructor..
function Obj($methods=array())
{
return new Obj($methods);
}
@VanTanev

This comment has been minimized.

VanTanev commented Sep 22, 2010

You come from a js background, right?

@klang

This comment has been minimized.

klang commented Sep 22, 2010

php 5.3 ?

@jubianchi

This comment has been minimized.

jubianchi commented Sep 22, 2010

This is interesting but the only problem is that PHP does not allow Closure serialisation.... so these classes won't be serializable....
To prevent this problem we would have to clean the 'methods' property when serializing and rebuild it on when unserializing.

@dhotson

This comment has been minimized.

Owner

dhotson commented Sep 22, 2010

Yep, PHP 5.3 only sorry.

Ah yeah, it really sucks that you can't serialise objects with closures. :-(
To be fair, you run into the same problem in Javascript.

@klang

This comment has been minimized.

klang commented Sep 22, 2010

yeah .. 5.3 .. I am actually looking forward to the day we upgrade our production environment to that version .. it's going to be a total map/reduce party!

@dhotson

This comment has been minimized.

Owner

dhotson commented Sep 22, 2010

Heh.. it's funny you should mention that. We recently deployed 5.3 on our production servers, which led me to thinking about what kind of crazy evil stuff I could implement using closures. This is the result.

@leeoniya

This comment has been minimized.

leeoniya commented Sep 22, 2010

a good amount of overhead, but you can get around the serialization problem via:
http://github.com/jeremeamia/super_closure

@dhotson

This comment has been minimized.

Owner

dhotson commented Sep 22, 2010

Hah, that's a pretty cool hack. /impressed

@jubianchi

This comment has been minimized.

jubianchi commented Sep 22, 2010

very interesting!

@Jalopy

This comment has been minimized.

Jalopy commented Sep 22, 2010

I like it :)

@brennen

This comment has been minimized.

brennen commented Sep 23, 2010

Neat.

@jbrumwell

This comment has been minimized.

jbrumwell commented Sep 23, 2010

Very cool, could be a good way to implement inceptors, sequences. tried an implementation http://gist.github.com/594135/ haven't tested it at all though

@Gorcyn

This comment has been minimized.

Gorcyn commented Sep 24, 2010

Definitely not OO !

@julioprotzek

This comment has been minimized.

julioprotzek commented Jan 21, 2011

Cool stuff!

@rhowardiv

This comment has been minimized.

rhowardiv commented Jul 1, 2011

Forked with view to providing a replacement for StdClass that allows you to attach callable methods to.

@igorzg

This comment has been minimized.

igorzg commented Sep 16, 2013

This pattern is mostly used in javascript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment