Created
June 3, 2012 11:38
-
-
Save slacken/2863144 to your computer and use it in GitHub Desktop.
在V2EX上看到的一道面试题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MagicObject{ | |
private $p = array(); | |
public function __set($name,$value){ | |
$this->p[$name] = $value; | |
} | |
public function __get($name){ | |
if(isset($this->p[$name]))return $this->p[$name]; | |
return FALSE; | |
} | |
public function __call($name,$aug){//$aug是数组 | |
if(method_exists($this,$name))return $this->$name($aug); | |
if(isset($this->p[$name]) && is_callable($this->p[$name])){ | |
//if($name == 'isOwnerTo')return $this->p[$name]($this,$aug[0]); | |
//@gee 的方法更好一些... | |
array_unshift($aug,$this); | |
return call_user_func_array($this->p[$name],$aug); | |
} | |
return FALSE; | |
} | |
} | |
$user = new MagicObject; | |
$user->isOwnerTo = function ($self, $car) { | |
return ($self->id == $car->owner_id); | |
}; | |
$car = new StdClass; | |
$car->owner_id; | |
$car->owner_id = 1; | |
$user->id = 1; | |
assert($user->isOwnerTo($car)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment