PHP Magic Constants Introduction and Magic function using code description Summary

Source: Internet
Author: User
Tags autoload
Magic Constants

1. Line
Returns the current line number in the file.

2. FILE
Returns the full path and file name of the file. If used in the include file, the include filename is returned. Since PHP 4.0.2, FILE always contains an absolute path, and the previous version sometimes contains a relative path.

3. FUNCTION
Returns the name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.

4. CLASS
Returns the name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, this value is always in lowercase letters.

5. METHOD
Returns the method name of the class (PHP 5.0.0 new). Returns the name of the method when it is defined (case-sensitive).

What is a magic function?

For the beginning of the function is named Magic function, such functions are triggered under certain conditions. For example: Set () get (), etc.
triggered when setting or taking a property that does not exist.
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 an object is instantiated, this method of the object is called first.
Destruct () This method is called when an object or object operation is terminated.

Class Test1 {public function construct () {var_dump (function),} public Function destruct () {Var_dump (function)}} $t 1 = new Test1; Unset ($t 1);

Get is called when attempting to read a property that does not exist.
Set is called when an attempt is made to write a value to a property that does not exist.
Isset is called when attempting to detect an attribute that does not exist.
Unset is called when an attempt is made to cancel an attribute that does not exist.

Class Test2 {public $name 3; public function set ($key, $value) {var_dump (function. ' KEY: '. $key. ' Value: '. $value); The Public function get ($key) {var_dump (function. ' KEY: '. $key); The Public Function isset ($key) {var_dump (function. ' KEY: '. $key); The 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 is called when the object is serialized
Wakeup called when an inverse object is being deserialized
One thing to note:
1. Sleep () must return an array or an 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 contents of the assignment, you need the property to return the array in sleep ().
Specified.
such as the difference between $id and $id2.

Class Test3 {Public $name = "Steven", Public $id = "1", public $id 2, public Function sleep () {var_dump (function);//serialization Unsuccessful. No return value. The deserialization also fails//return Array ("name"); Serialization succeeded. There is a return value. The Id2 property is restored//return array ("name", "Id2");//serialization succeeds. There is a return value. The Id2 property cannot be restored to the return array ("name"); Public Function Testecho () {var_dump ($this->name); Var_dump ($this->id); Var_dump ($this->id2); function Wakeup () {var_dump (function); $this->testecho ();}} $t 3= new Test3; $t 3->id2 = Uniqid (); $t 3s = serialize ($t 3); Unserialize ($t 3s);

ToString This method will be called when you print an object directly

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 called when attempting to invoke a method that does not exist.
This method must have two parameters, the first is the method name to call, and the second is an array of arguments for the called method.
It is important to note that when you invoke the private method of the parent class in a subclass, or call the class's non-protect method in the instance
Call () is not invoked

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 () Called when attempting to invoke a static method that does not exist
This method must have two parameters, the first is the method name to call, and the second is an array of arguments for the called method.
appear in the PHP5.3

Class Test51 {public Function callstatic ($fun, $param) {var_dump (' function: '. $func); Var_dump ($param);}} test51::test ( ' xx ', ' xx ', ' xx ');

Set_state () is called when an 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

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 called when the instance is cloned.
Attention:
1. In PhP5, assignment between objects is always passed as an address reference.
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 an instance is also an instance, then this member property is
Passed to the new instance.
Assignments between objects are always passed with an address reference. The Age property of $t $t 72 is the same.

Class Test71 {public $age = ten;} $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 = clone $t 71; $t 71->age = 13; Var_dump ($t 71->age); Var_dump ($t 73->age); If a member property in an instance is also an instance, the member property is passed to the new instance as a reference method.

The AutoLoad () function. When creating an instantiation, if the corresponding class does not exist, it is called

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.