PHPで特異メソッド(っぽいもの)
PHP5.3のラムダが普及すればわりと使えるかも。
クラス定義
<?php class A { function __call($name, $args) { $method = $this->$name; array_unshift($args, $this); // 自分自身を第一引数とする return call_user_func_array($method, $args); } }
実行
<?php $a = new A(); // メソッド定義。第一引数にはオブジェクトが渡される $a->hello = create_function('$self', ' echo "Hello, " . $self->str . "\n"; '); $a->str = 'World'; $a->hello(); // 'Hello, World'が出力される