Magic Method Review
2014-9-2 10:08:00 notepad++ by Jiancaigege Fei Hong Shadow ~
=========================
1,__construct ()
Construction method
Methods to run automatically when a class is instantiated
Commonly used to initialize member property values, etc.
2,__destruct ()
Destructors
Methods to run automatically when a class is destroyed
Destroying resources, closing resources, etc.
3, __set ($param, $value)
method to be called automatically when setting a value for a non-public member property in an object
For example:
1 Public function __set ($param,$value) {2$this,$param= $value ; 3 }
4,__get ($param)
method to be called automatically when retrieving non-public member property values in an object
For example:
1 Public function __get ($param) {2return$this-$param ; 3 }
5,__isset ()
method to be called automatically when testing non-public property values in an object
6,__unset ()
method to automatically invoke when destroying non-public property values in an object
7,__tostring ()
Methods that are called automatically when you try to directly echo an object
Example:
1 Public function __tostring () {2echo "This is a test class." ; 3 }
8,__call ()
Method that is called automatically when an attempt is made to invoke a method that does not exist in the object
Example:
1 Public function __call ($funname,$args) {2echo "The method you called {$funname } does not exist. The parameter is an array {$args} "; 3 }
9,__clone ()
When cloning an object, there are child objects or resources in the object, this method needs to be added at this time, otherwise it will be copied incomplete
Of course, we can also add trigger_error (' Error ', E_user_error) to this method, or set it directly to private to prevent cloning
10,__sleep ()
When an object is serialized (serialize), if there are child objects or resources in the class being called;
At this point we need to use this method to commit uncommitted data, or similar cleanup operations;
such as saving data, closing some resources, etc.
Example: (Data required to save a connected database when serializing)
1 Private functionConnect ()2 {3 $thislink =mysql_connect($thisServer,$thisUsername,$this-password);4 mysql_select_db($this-DB,$this-link);5 }6 7 Public function__sleep ()8 {9 return Array(' Host ', ' username ', ' password ', ' db ' );Ten}
11,__wakeup ()
When an object is crossdress (unserialize), it restores some state before serialization, such as re-establishing the database connection or performing other initialization operations
Example: (In the above example, we need to automatically restore the database connection)
1 Public function __wakeup () 2 {3$this , connect (); 4 }
12,__autoload (String $class _name)
For automatically loading the required class files
Note: This method is used outside of the class
Example:
1 function__autoload ($classname){2 $name=$classname.". Class.php ";3 if(file_exists($name)){4 require($name);5 Echo* * * Automatically loaded <font color= ' red ' > '.$name." </font> category <br> ";6}Else{7 die("<font color= ' Red ' >".$name." </font> class does not exist! ");8 }9 }Ten One $m=NewDbmysql ("Stu");//the DbMysql.class.php and DbMysql.class.php referenced or inherited parent files are automatically loaded in the current directory A //$m =new dbmysqli ("Stu");//This will automatically load the current directory DbMysqli.class.php - //$m =new dbsqlite ("Stu");//This will automatically load the current directory DbSqlite.class.php
PHP Object-Oriented Magic method review