Php magic Functions

Source: Internet
Author: User

What is a magic function?
For functions starting with _, they are named Magic functions. These functions are triggered under specific conditions, such as _ set () _ get ().
Triggered when setting or retrieving nonexistent attributes.
What about the magic functions?
In general, there are several magic Functions
_ Construct () _ destruct () _ get () _ set () _ isset () _ unset () _ call () _ callStatic ()
_ Sleep () _ wakeup () _ toString () _ set_state () _ clone () _ autoload ()
_ Construct () when instantiating an object, this method of this object is called first.
_ Destruct () This method is called when an object or object operation is terminated.
Copy codeThe Code is as follows:
Class test1 {
Public function _ construct (){
Var_dump (_ function __);
}
Public function _ destruct (){
Var_dump (_ function __);
}
}
$ T1 = new test1;
Unset ($ t1 );

_ Get is called when an attempt is made to read an attribute that does not exist.
_ Set is called when you try to write a value to an attribute that does not exist.
_ Isset is called to detect an attribute that does not exist.
_ Unset is called to cancel an attribute that does not exist.
Copy codeThe Code is as follows:
Class test2 {
Public $ name3;
Public function _ set ($ key, $ value ){
Var_dump (_ function __.'
KEY :'
. $ Key .'
Value :'
. $ Value );
}
Public function _ get ($ key ){
Var_dump (_ function _. 'key: '. $ KEY );
}
Public function _ isset ($ key ){
Var_dump (_ function _. 'key: '. $ KEY );
}
Public function _ unset ($ key ){
Var_dump (_ function _. 'key: '. $ KEY );
}
}
$ T = new test2;
$ T-> name = "steven ";
$ T-> name2;
$ T-> name3;
Isset ($ t-> name2 );
Isset ($ t-> name3 );
Unset ($ t-> name4 );

_ Sleep called when the object is serialized
_ Wakeup is called when an anti-sequential object is executed.
Note that:
1. _ sleep () must return an array or object (usually $ this is returned), and the returned value will be used as a serialized
Value.
If this value is not returned, serialization fails. This also means that deserialization will not trigger the _ wakeup event.
2. serialization will save the default value assignment attribute. If you want to instantiate the value assignment content, the attribute must be in _ sleep () to return the array's
Specified.
For example, the difference between $ id and $ id2.
Copy codeThe Code is as follows:
Class test3 {
Public $ name = "steven ";
Public $ id = "1"; public $ id2;
Public function _ sleep (){
Var_dump (_ function _); // serialization failed. No return value. Reverse Sequence also failed //
Return array ("name"); // serialization succeeded. return value exists. Reverse Sequence succeeded. The id2 attribute can be restored //
Return array ("name", "id2"); // serialization successful. returned value exists. The reverse sequence is successful. The id2 attribute cannot be restored.
Return array ("name ");}
Public function testEcho (){
Var_dump ($ this-> name );
Var_dump ($ this-> id );
Var_dump ($ this-> id2 );
}
Public function _ wakeup (){
Var_dump (_ function __);
$ This-> testEcho ();
}
}
$ T3 = new test3;
$ T3-> id2 = uniqid ();
$ T3s = serialize ($ t3 );
Unserialize ($ t3s );

_ ToString this method will be called when an object is printed directly.
Copy codeThe Code is as follows:
Class test4 {
Public function _ toString (){
Return "toString ";
}
}
$ T4 = new test4 ();
Echo $ t4;
Print $ t4;
Var_dump ($ t4 );
Print_r ($ t4 );

_ Call ($ func, $ param) is called when you try to call a method that does not exist.
This method must have two parameters: the first is the name of the called method, and the second is a parameter array of the called method.
It should be noted that when you call the private method of the parent class in a subclass or call the non-protect method of the class in the instance
Does not call _ call ()
Copy codeThe Code is as follows:
Class test5 {
Public function _ call ($ func, $ param ){
Var_dump ('function: '. $ func );
Var_dump ($ param );
}
}
$ T5 = new test5;
$ T5-> echoTest ('XX', 'XX', 'xx ');

_ CallStatic () is called when you try to call a non-existent static method.
This method must have two parameters: the first is the name of the called method, and the second is a parameter array of the called method.
In PHP5.3
Copy codeThe Code is as follows:
Class test51 {
Public function _ callStatic ($ fun, $ param ){
Var_dump ('function: '. $ func );
Var_dump ($ param );
}
}
Test51: test ('XX', 'XX', 'xx ');

_ Set_state () is called when var_export is used to export an instance. This method has a parameter that contains
An array with member attributes
Copy codeThe Code is as follows:
Class test6 {
Public function _ set_state ($ arr ){
Var_dump ($ arr );
}
}
$ T6 = new test6;
$ T6-> age = "12 ";
Var_export ($ t6, true );
Var_export ($ t6 );
Eval ('
$ B ='
. Var_export ($ t6, true ).';');
Print_r ($ B );

_ Clone () is called when an instance is cloned.
Note:
1. In php5, the assignment between objects is always passed by address reference.
2. If you want to pass it by actual value, you need to use the clone keyword.
3. clone only instances. If a member attribute in the instance is also an instance, the member attribute will still be referenced
To the new instance.
// The Value assignment between objects is always transmitted by address reference. $ t71 $ t72 has the same age attribute.
Copy codeThe Code is as follows:
Class test71 {
Public $ age = 10;
}
$ T71 = new test71 ();
$ T72 = $ t71;
Var_dump ($ t71-> age );
$ T71-> age = 12;
Var_dump ($ t71-> age );
Var_dump ($ t72-> age); // if you want to pass with the actual value, you need to use the clone keyword $ t73 = clone $ t71; $ t71-> age = 13; var_dump ($ t71-> age );
Var_dump ($ t73-> age); // If a member attribute in the instance is also an instance, the member attribute will still be passed to the new instance as a reference method.

Copy codeThe Code is as follows:
Class test74 {
Public $ age = 10;
Public $ sub = null;
}
Class test75 {
Public $ age = 11;
}
$ I = new test74;
$ I-> sub = new test75 ();
$ I1 = clone $ I;
Var_dump ($ i1-> sub-> age );
$ I-> sub-> age = 12;
Var_dump ($ i1-> sub-> age );

// Although $ I and $ i1 do not point to the same instance, their member attribute $ sub points to the same instance. At this time, we must borrow
Run the _ clone method to copy $ sub. // $ I2 and $3 point to different instances. Member attributes $ sub also point to different instances.
Copy codeThe Code is as follows:
Class test76 {
Public $ age = 10;
Public $ sub = null;
Public function _ clone (){
$ This-> sub = clone $ this-> sub;
}
}
$ I2 = new test76 ();
$ I2-> sub = new test75 ();
$ I3 = clone $ i2;
$ I2-> sub-> age = 15;
Var_dump ($ i3-> sub-> age );

_ Autoload () function. When an instantiation is created, if the corresponding class does not exist, it will be called.
Copy codeThe Code is as follows:
Function _ autoload ($ class ){
If ($ class = "test8 "){
Require_once dirname (_ FILE _). '/class8.php ';
}
}
Spl_autoload ();
$ T8 = new test8;
Var_dump ($ t8-> age );

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.