Magic constant:
1. _ LINE __
Returns the current row number in the file.
2. _ FILE __
Returns the complete file path and file name. If it is used in a include file, the include file name is returned. From PHP4.0.2, __file _ always contains an absolute path, while previous versions sometimes contain a relative path.
3. _ FUNCTION __
Returns the function name (New in PHP4.3.0 ). Starting from PHP5, this constant returns the name (case sensitive) when the function is defined ). In PHP4, the value is always lowercase letters.
4. _ CLASS __
The name of the returned class (New in PHP4.3.0 ). Starting from PHP5, this constant returns the name (case sensitive) when the class is defined ). In PHP4, the value is always lowercase letters.
5. _ METHOD __
Method Name of the returned class (New PHP5.0.0 ). Returns the name (case sensitive) when the method is defined ).
Magic functions:
1. _ Construct ()
Constructor: called when an object is instantiated,
When _ construct and the constructor with the class name exist at the same time, __construct will be called, and the other will not be called.
4. _ Get ()
When reading an object property, if the property exists, the property value is directly returned. If the property does not exist, the _ get function is called.
5. _ Set ()
When setting attributes of an object,
If an attribute exists, the value is assigned directly;
If not, the _ set function is called.
6. _ ToString ()
It is called to print an object. Such as echo $ obj; or print $ obj;
7. _ Clone ()
Called when cloning an object. For example: $ t = newTest (); $ t1 = clone $ t;
8. _ Sleep ()
Serialize is called before. If the object is large and you want to delete something and serialize it, consider this function.
9. _ Wakeup ()
Unserialize is called to initialize objects.
10. _ Isset ()
It is called to check whether an object property exists. For example, isset ($ c-> name ).
11. _ Unset ()
Unset an object property is called. For example, unset ($ c-> name ).
12. _ Set_state ()
Called when var_export is called. Use the return value of _ set_state as the return value of var_export.
13. _ Autoload ()
When instantiating an object, if the corresponding class does not exist, this method is called.
First knowledge magic methods
Since the release of Php5.0, many object-oriented features have been provided for us, especially a lot of easy-to-use magic methods. These magic methods allow us to simplify our coding, better design our system. Today, let's get to know the magic methods provided by php5.0.
PHP | magic method |__ toString () ,__ clone () ,__ call () ,__ autoload ()
_ ToString ()
If I have a class:
ClassPerson
{
Private $ name = "";
Private $ age = 0;
Function _ construct ($ name = "", $ age = "")
{
$ This-> name = $ name;
$ This-> age = $ age;
}
Functionsay ()
{
Echo "name:". $ this-> name. "<br/>". "age:". $ this-> age. "<br/> ";
}
}
Now I will instantiate this class and print this instance:
$ P1 = new person ("liuzy", 20 );
Echo $ p1; // error when Printing Directly
Obviously, it is wrong to print the object directly because the object is a reference handle and cannot be printed directly. In this case, we can use the _ toString () method. We add a _ toString () method to the Person class:
Function _ toString ()
{
Return "I am Person, my name is". $ this-> name. "<br/> ";
}
Then refresh the page and find something?
Now we understand that __tostring () is the method executed when the object is printed directly. We can use this method to print some information about the class.Note: There are two underscores (_), and the method must return values.
_ Clone ()
We know that objects can be directly assigned values, for example
$ P2 = $ p1; // here an object has two references
Then execute:
$ P1-> say ();
$ P2-> say ();
Yes, and the effect is the same.
We have another method:
$ P3 = clone $ p1; // note that clone is a clone keyword. the difference here is that $ p3 is a new object.
At the same time, we add a method to the class:
Function _ clone ()
{
$ This-> name = "I Am a copy"; // Note: $ this is the cloned object, not the current class.
}
Then we execute:
$ P3-> say ();
Print:
Name: I am a copy
Age: 20
Here we understand that the __clone () method is executed when the object is cloned. It is used to initialize attributes of the new cloned copy.
_ Call ()
The main function of this method is to execute the _ call () method when the instance of this class calls a non-existent method. Note that you must declare it in the class in advance:
Function _ call ($ fname, $ argus)
{
Echo "The method you called:". $ fname. "does not exist <br/> ";
Echo "parameter is". print_r ($ argus );
}
_ Autoload ()
When calling a class, we must first introduce the file where the class is located (include "xxx. php). If we call many classes on a page, we have to use many include "xxx. php ". Obviously, this is very troublesome.
The _ autoload () method can help us solve this problem.
For example, we define the file of the Person class above as Person_class.php,
Create another php file test. php and edit the content:
Function _ autoload ($ calssName)
{
Include $ className. "_ class. php"; // you may understand this? Haha
}
$ P = new Person ("mifan", 22 );
$ P-> say ();
In this way, the test. php page will not contain errors.
The _ autoload () method is called when a class does not exist in its life. It has a string type parameter that declares the Class Name of the class that does not exist.
Of course, the naming of class files is also very exquisite. It is better to have a relationship with the class, such as Person_class.php.