<?PHPclassStdobject { Public function__construct (Array $arguments=Array()) {if(!Empty($arguments)) {foreach($arguments as $property=$argument) {$this->{$property} =$argument;}}} Public function__call ($method,$arguments) {$arguments=Array_merge(Array("Stdobject" =$this),$arguments);//note:method argument 0 would always be referred to the main class ($this).if(isset($this->{$method}) &&is_callable($this->{$method})) {return Call_user_func_array($this->{$method},$arguments);} Else {Throw New Exception("Fatal error:call to undefined method stdobject::{$method}()");}}}//Usage.$obj=NewStdobject ();$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;};$func= "Setage";$obj->{$func} =function($stdObject,$age) {//$age is the first parameter passed if calling this method.$stdObject->age =$age;};$obj->setage (24);//Parameter value passing to the $age argument in method ' Setage () './/Create dynamic method. Here I ' m generating getter and setter dynimically//Beware:method name is case sensitive.foreach($obj as $func _name=$value) {if(!$valueinstanceof Closure) {$obj->{"Set".Ucfirst($func _name)} =function($stdObject,$value) Use($func _name) {//note:you can also use keyword ' use ' to bind parent variables.$stdObject->{$func _name} =$value;};$obj->{"Get".Ucfirst($func _name)} =function($stdObject) Use($func _name) {//note:you can also use keyword ' use ' to bind parent variables.return $stdObject->{$func _name};};}}$obj->setname ("John");$obj->setadresse ("Boston");$obj->getinfo ();
PHP Stdclass Example