才知道php可以動態建立屬性,可以跟javascript一樣.
Php程式碼
class Book{ public $name; public function __construct($name) { $var= func_get_arg(0); if(is_int($var)){ $this->name="12345".$name; } if(is_string($name)){ $this->name=$name; } } } class Main{ public static function createbook($class,$config=null){ return new $class($config); } public function config($config){ if(is_array($config)){ foreach($config as $key=>$val){ $this->$key=$val; } } } } $config=array( 'name'=>'My Web Application', ); $main=new Main(); $main->config($config); echo $main->name;
結果輸出"My Web Application";
而且php的重載是透過func_get_arg(0) ,func_arg(0) ,(func_args(s(func_arg(0) ,(func_arg(0) ,(func_arg(0) ,(func_arg(0) ,(func)來實現的.