Skip to content

Instantly share code, notes, and snippets.

@ziadoz ziadoz/meta.php forked from funkatron/foo.php
Created Feb 29, 2012

What would you like to do?
PHP 5.4 Meta Programming?
// See:
trait Call_Dynamic_Methods
public function __call($name, $arguments)
if (isset($this->{$name}) && $this->{$name} instanceof Closure) {
$this->{$name} = $this->{$name}->bindTo($this, $this);
return call_user_func_array($this->{$name}, $arguments);
return parent::__call($name, $arguments);
class Meta
use Call_Dynamic_Methods;
public $name;
public static $colours = array('red', 'green', 'blue');
public function __construct()
foreach (static::$colours as $colour) {
$functionName = 'name' . ucfirst($colour);
$this->{$functionName} = function() use ($colour) {
return '<span style="color: ' . $colour . '">' . $this->name . '</span>';
$meta = new Meta;
$meta->name = 'John Smith';
echo $meta->nameGreen();
echo $meta->nameRed();
$meta->name = 'Joe Bloggs';
echo $meta->nameBlue();
Could this be taken a step further by adding functions to trait for creating and removing methods on the fly?
Then something like Ruby's attr_accessor functionality (and more) could be recreated?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.