时间:2021-07-01 10:21:17 帮助过:24人阅读
Object.prototype.greeting = 'Hello' var o = new Object alert(o.greeting)
error_reporting(E_ALL);
class Object
{
public static $prototype;
protected function __get($var) {
if ( isset(self::$prototype->$var) ) {
return self::$prototype->$var; }}
}
Object::$prototype->greeting = 'Hello'; $o = new Object; echo $o->greeting; //输出 Hello
Object.prototype.say = function(word) { alert(word) }
o.say('Hi')
error_reporting(E_ALL);
class Object
{
public static $prototype;
protected function __get($var) {
if ( isset(self::$prototype->$var) ) {
return self::$prototype->$var; }}
protected function __call($call, $params) {
if ( isset(self::$prototype->$call) && is_callable(self::$prototype->$call) ) {
return call_user_func_array(self::$prototype->$call, $params); }
else {
throw new Exception('Call to undefined method: ' . __CLASS__ . "::$call()"); }}
}
Object::$prototype->say = create_function('$word', 'echo $word;');
$o->say('Hi');
Object.prototype.rock = function() { alert(this.oops) }
o.oops = 'Oops'
o.rock()
Object::$prototype->rock = create_function('', 'echo $this->oops;');
$o->oops = 'Oops';
$o->rock();
Object::$prototype->rock = create_function('$caller', 'echo $caller->oops;');
$o->oops = 'Oops';
$o->rock($o);
function create_method($args, $code) {
if ( preg_match('/\$that\b/', $args) ) {
throw new Exception('Using reserved word \'$that\' as argument'); }
$args = preg_match('/^\s*$/s', $args) ? '$that' : '$that, '. $args;
$code = preg_replace('/\$this\b/', '$that', $code);
return create_function($args, $code); }
class Object
{
public static $prototype;
protected function __get($var) {
if ( isset(self::$prototype->$var) ) {
return self::$prototype->$var; }}
protected function __call($call, $params) {
if ( isset(self::$prototype->$call) && is_callable(self::$prototype->$call) ) {
array_unshift($params, $this); // 这里!
return call_user_func_array(self::$prototype->$call, $params); }
else {
throw new Exception('Call to undefined method: ' . __CLASS__ . "::$call()"); }}
}
Object::$prototype->rock = create_method('', 'echo $this->oops;');
$o->oops = 'Oops';
$o->rock();
class Object
{
public static $prototype;
protected function __get($var) {
... }
protected function __call($call, $params) {
... }
}
class Test extends Object
{
}
Test::$prototype->greeting = 'Hello';
print_r(Object::$prototype);
/* outputs
stdClass Object
(
[greeting] => Hello
)
*/
Test::$prototype->say = create_method('$word', 'echo $word;');
$o = new Object;
$o->say('Hi');
/* outputs
Hi
*/