PHP Stdclass Example

Source: Internet
Author: User

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.