PHP Magic Method and a comprehensive explanation of _php instances and connected instances

Source: Internet
Author: User
<?php//Magic Method//When containing multiple classes//1. Magic method for automatic loading of classes __autoload () function __autoload ($classname) {if (Isset ($classname)) {require_ Once $classname. " Class.php ';}} /* $computer 1=new computer (), $computer 1->addlist (' Dalisng ', 234); Echo $computer 1; *///__call () mask the error generated when invoking the method, and when we call a nonexistent method, the __call () method is called automatically. Independent example, two instances were established, each non-interference $computer2=new computer (); Echo $computer 2->name;echo "
"; $computer 3=new computer (); Echo $computer 3->name;echo"
$computer 3->name= "Big bright"; Echo ' $computer 3->name: '. $computer 3->name;echo "
"Echo ' $computer 2->name: '. $computer 2->name;echo"
"; Echo" ";///connected instance, that is, with the equals sign to open a new instance, at this time two instances together point to a memory address, a loss, a wing Rong $computer4=new computer (); Echo ' $computer 4->name: '. $ Computer4->name;echo "
"; $computer 5= $computer 4;echo ' $computer 5->name: '. $computer 5->name;echo"
$computer 4->name= "Big bright"; Echo ' $computer 4->name: '. $computer 4->name;echo "
"Echo ' $computer 5->name: '. $computer 5->name;echo"
"; Echo" ";//Clone __clone, the clone of the new instance and the cloned instance is two independent individuals, although the property value is the same, but the address of the property store is different, so after cloning you and I have nothing to do $computer6=new computer (); Echo ' $computer 6- >name: '. $computer 6->name;echo '
"; $computer 7=clone $computer 6;echo ' $computer 7->name: '. $computer 7->name;echo"
"; $computer 6->name=" clone "; Echo ' $computer 6->name: '. $computer 6->name;echo"
"Echo ' $computer 7->name: '. $computer 7->name;echo"
";

Computer.class.php

<?phpclass computer{public $name = "1234"; function __construct () {echo "You is right!";} function __call ($methodName, $argsList) {//This method is called automatically when the method does not exist, $argsList is the corresponding parameter echo $methodName. " () method does not exist! "; echo "
";p Rint_r ($argsList); echo"
";} Private Function __tostring () {///when the user outputs a string that does not exist, the method automatically calls the Echo object name echo "I am the object's string!" ";}}

The above is a small series for everyone to bring the PHP magic method and about the independent instances and connected instances of the full explanation of all the content, I hope you support the script home ~

  • Related Article

    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.