<?php
Magic method
When multiple classes are included
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 instances, each of the two instances established, non-interfering
$computer 2=new computer ();
echo $computer 2->name;echo "<br/>";
$computer 3=new computer ();
echo $computer 3->name;echo "<br/>";
$computer 3->name= "Big bright";
Echo ' $computer 3->name: '. $computer 3->name;echo "<br/>";
Echo ' $computer 2->name: '. $computer 2->name;echo "<br/>";
echo "Connected instances, that is, with the equals sign to open up a new instance, at this time two instances together point to a memory address, a loss, a wing Rong
$computer 4=new computer ();
Echo ' $computer 4->name: '. $computer 4->name;echo "<br/>";
$computer 5= $computer 4;
Echo ' $computer 5->name: '. $computer 5->name;echo "<br/>";
$computer 4->name= "Big bright";
Echo ' $computer 4->name: '. $computer 4->name;echo "<br/>";
Echo ' $computer 5->name: '. $computer 5->name;echo "<br/>";
echo "Cloning __clone, the clone of the new instance and the cloned instance is two independent individuals, although the property values are the same, but the address of the property store is different, so after cloning you and I have nothing to do
$computer 6=new computer ();
Echo ' $computer 6->name: '. $computer 6->name;echo "<br/>";
$computer 7=clone $computer 6;
Echo ' $computer 7->name: '. $computer 7->name;echo "<br/>";
$computer 6->name= "cloning";
Echo ' $computer 6->name: '. $computer 6->name;echo "<br/>";
Echo ' $computer 7->name: '. $computer 7->name;echo "<br/>";
——————————————————————————————————————————————————————————————————————————————
Computer.class.php
<?php
Class computer{
Public $name = "1234";
function __construct () {
echo "You are 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 "<pre>";
Print_r ($argsList);
echo "</pre>";
}
Private Function __tostring () {//When the user outputs a string that does not exist, the method automatically calls such as the Echo object name
echo "I am the object of the string!" ";
}
}
PHP Magic methods and explanations of independent instances and connected instances