- phpversion: documentation ( source)
- print_r: documentation ( source)
- array_merge: documentation ( source)
- is_callable: documentation ( source)
<?php
class stdObject{
public function __construct(array $params=array()){
if(!empty($params))
foreach ($params as $key => $value)
$this->{$key}=$value;
}
public function __call($methodName,$params){
$params=array_merge(array('stdObject'=>$this),$params);
if(isset($this->{$methodName})&&is_callable($methodName)){
//$params就是函数的参数,这里的stdObject就是function中的$stdObject;
return call_user_func_array($methodName,$params);
} else {
throw new Exception("Fatal error: Call to undefined method stdObject::{$methodName}()");
}
}
}
echo phpversion();
$obj = new stdObject();
$obj->name = "Nick";
$obj->surname = "Doe";
$obj->age = 20;
$obj->adresse = null;
$obj->getInfo = function($stdObject) {
// $stdObject referred to this object (stdObject).
echo $stdObject->name . " " . $stdObject->surname . " have " . $stdObject->age . " yrs old. And live in " . $stdObject->adresse;
};
print_r($obj);
$obj->getInfo();