PHP Magic Function Usage Instructions _php basics

Source: Internet
Author: User
What is a magic function?
The functions that begin with __ are named Magic Functions, which are triggered under certain conditions. For example: __set () __get (), etc.
triggered when setting or fetching properties that do not exist.
What are the Magic functions?
In general, there are a few 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 invoked first.
__destruct () This method is invoked when an object or object operation is terminated.
Copy Code code as follows:

Class Test1 {
Public Function __construct () {
Var_dump (__function__);
}
Public Function __destruct () {
Var_dump (__function__);
}
}
$t 1 = new Test1;
Unset ($t 1);

__get is invoked when an attempt is made to read an attribute that does not exist.
__set is invoked when an attempt is made to write a value to a property that does not exist.
__isset is invoked when an attempt is made to detect an attribute that does not exist.
__unset is invoked when an attempt is made to cancel an attribute that does not exist.
Copy Code code as follows:

Class Test2 {
Public $name 3;
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 When a serialized object is called
__wakeup when a reverse sequence object is invoked
One thing to note:
1. __sleep () must return an array or object (typically returned by $this), 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 saves the properties of the default assignment. If you want to instantiate the assigned content, you need the property to return an array of __sleep ()
Specified.
such as the difference between $id and $id2.
Copy Code code as follows:

Class Test3 {
Public $name = "Steven";
Public $id = "1"; public $id 2;
Public Function __sleep () {
Var_dump (__function__); Serialization was unsuccessful. No return value. Reverse sequence also fails//
Return Array ("name"); Serialization succeeded. There is a return value. The reverse sequence was successful. ID2 Properties recovered//
Return Array ("name", "Id2");//serialization succeeded. There is a returned value. The Id2 property 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 ();
}
}
$t 3= new Test3;
$t 3->id2 = Uniqid ();
$t 3s = serialize ($t 3);
Unserialize ($t 3s);

__tostring when you print an object directly, this method will be called
Copy Code code as follows:

Class Test4 {
Public Function __tostring () {
return "toString";
}
}
$t 4 = new test4 ();
Echo $t 4;
Print $t 4;
Var_dump ($t 4);
Print_r ($t 4);

__call ($func, $param) is invoked when an attempt is to invoke a method that does not exist.
This method must have two parameters, the first is the calling method name, and the second is an array of arguments for the called method.
It should be noted that when you invoke the private method of the parent class in a subclass, or call the class's Protect method in the instance.
Time, and will not call __call ()
Copy Code code as follows:

Class Test5 {
Public Function __call ($func, $param) {
Var_dump (' Function: '; $func);
Var_dump ($param);
}
}
$t 5 = new Test5;
$t 5->echotest (' xx ', ' xx ', ' xx ');

__callstatic () is invoked when trying to invoke a static method that does not exist
This method must have two parameters, the first is the calling method name, and the second is an array of arguments for the called method.
Appearing in the PHP5.3
Copy Code code as follows:

Class Test51 {
Public Function __callstatic ($fun, $param) {
Var_dump (' Function: '; $func);
Var_dump ($param);
}
}
Test51::test (' xx ', ' xx ', ' xx ');

__set_state () is invoked when the instance is exported with Var_export. This method has a parameter that is the one that contains the exported instance
An array that has member properties
Copy Code code as follows:

Class Test6 {
Public Function __set_state ($arr) {
Var_dump ($arr);
}
}
$t 6 = new Test6;
$t 6->age = "12";
Var_export ($t 6, true);
Var_export ($t 6);
Eval ('
$b = '
. Var_export ($t 6,true);
Print_r ($b);

__clone () is invoked when cloning an instance.
Attention:
1. In php5, assignments between objects are always passed by address references.
2. If you want to pass the actual value, you need to use the Clone keyword
3.clone is just an example. If a member property in the instance is also an instance, then the member property is still referenced by the
Passed to the new instance.
An assignment between objects is always passed by an address reference. $t $t 72 of The age attribute is the same.
Copy Code code as follows:

Class Test71 {
Public $age = 10;
}
$t = new test71 ();
$t $t 71;
Var_dump ($t 71->age);
$t 71->age = 12;
Var_dump ($t 71->age);
Var_dump ($t 72->age); If you want to pass the actual value, you need to use the Clone keyword $t the clone $t 71; $t 71->age = 13; Var_dump ($t 71->age);
Var_dump ($t 73->age); If a member property in the instance is also an instance, the member property is passed to the new instance as a reference method.

Copy Code code as follows:

Class Test74 {
Public $age = 10;
public $sub = null;
}
Class Test75 {
Public $age = 11;
}
$i = new test74;
$i->sub = new Test75 ();
$i 1 =clone $i;
Var_dump ($i 1->sub->age);
$i->sub->age = 12;
Var_dump ($i 1->sub->age);

$i and $i1 do not point to the same instance, but their member properties $sub point to the same instance. At this time, we must borrow
Help __clone this method to replicate $sub. $i 2 and $ to point to different instances. member property $sub also points to different instances.
Copy Code code as follows:

Class Test76 {
Public $age = 10;
public $sub = null;
Public Function __clone () {
$this->sub = Clone $this->sub;
}
}
$i 2 = new test76 ();
$i 2->sub = new Test75 ();
$i 3 = clone $i 2;
$i 2->sub->age = 15;
Var_dump ($i 3->sub->age);

__autoload () function. When an instantiation is created, if the corresponding class does not exist, it is called
Copy Code code as follows:

function __autoload ($class) {
if ($class = = "Test8") {
Require_once dirname (__file__). ' /class8.php ';
}
}
Spl_autoload ();
$t 8 = new Test8;
Var_dump ($t 8->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.