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 file name is returned. Since PHP4.0.2, __file__ always contains an absolute path, and the previous version sometimes contains a relative path.
3. __function__
Returns the function name (PHP4.3.0 new). Since PHP5 this constant returns the name of the function when it is defined (case-sensitive). This value is always lowercase in PHP4.
4. __class__
Returns the name of the class (PHP4.3.0). Since PHP5 this constant returns the name of the class when it is defined (case-sensitive). This value is always lowercase in PHP4.
5. __method__
Returns the method name of the class (PHP5.0.0). Returns the name (case-sensitive) of the method when it is defined.
Magic Function:
1. __construct ()
Constructor: Called when an object is instantiated,
When __construct and a constructor with the class name as the function name exist at the same time, the __construct is invoked and the other is not invoked.
4. __get ()
When a property of an object is read, the property value is returned directly, or the __get function is called if it does not exist.
5. __set ()
When you set the properties of an object,
If the attribute exists, the value is directly assigned;
If it does not exist, the __set function is called.
6. __tostring ()
When an object is printed, it is invoked. such as echo$obj; or print$obj;
7. __clone ()
Called when the object is cloned. such as: $t =newtest (); $t 1=clone $t;
8. __sleep ()
Serialize before being invoked. If the object is relatively large, want to cut a little bit of the serialization, you can consider this function.
9. __wakeup ()
Unserialize is invoked to do initialization work on some objects.
10. __isset ()
Called to detect whether an object's properties exist. such as: Isset ($c->name).
11. __unset ()
Called when a property of an object is unset. such as: unset ($c->name).
12. __set_state ()
Called when the Var_export is invoked. Use the return value of the __set_state as the return value of the Var_export.
13. __autoload ()
When an object is instantiated, the method is invoked if the corresponding class does not exist.
The method of the first Magic
Php5.0 has provided us with a lot of object-oriented features since it was released, especially to provide us with a lot of easy-to-use magic methods that allow us to simplify our coding and better design our systems. Today we come to know the Magic method that php5.0 offers us.
php| Magic Method |__tostring (), __clone (), __call (), __autoload () detailed
__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'm going to instantiate this class and then print this example:
$p 1= New Person ("Liuzy", 20);
echo $p 1; Direct printing can be an error
Obviously this is an error in printing the object directly, because the object is a reference handle and cannot be printed directly. At this point, 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/> ";
}
and then refresh the page, what did you find?
Now we understand that __tostring () is the method that is executed when the object is printed directly, and we can use this method to print some relevant information about the class. Note: Two underscores, the method must have a return value.
__clone ()
We know that objects can be directly assigned, such as
$p 2= $p 1; Here is an object with two references
Then I perform:
$p 1->say ();
$p 2->say ();
Can be performed, and the effect is the same.
We also have one way:
$p 3= clone $p 1; Note that clone is a cloning keyword, and here the difference is that $P3 is a new object.
And we add a method to the class:
Function__clone ()
{
$this->name = "I am a copy"; Note: The $this here is the object itself that the clone produces, not the current class
}
And then we execute:
$p 3->say ();
Print out:
Name: I am a copy
Age:20
Here we understand that the __clone () method is a method that is executed when the object is cloned, and its role is to initialize the newly cloned replica for property initialization.
__call ()
The primary function of this method is to execute the __call () method when an instance of the class invokes a method that does not exist. Note that you need to declare in advance in the class:
Function__call ($fname, $argus)
{
echo "The method you call:". $fname. " Does not exist <br/> ";
echo "parameter is". Print_r ($argus);
}
__autoload ()
When we call a class at ordinary times, we must first introduce the file that contains the class (include "xxx.php"), and if we call a lot of classes on a page, we have to use many include "xxx.php". Obviously this is very troublesome.
__autoload () method can help us solve this problem.
For example, we define the file of the person class above as person_class.php,
Then create a new php file test.php, edit the content:
function __autoload ($calssName)
{
Include $className. " _class.php "; You see this, maybe you get it? Ha ha
}
$p = new Person ("Mifan", 22);
$p->say ();
This will not be an error if you execute the test.php page.
The __autoload () method is a method that is invoked when a class does not exist, and it has a string parameter that declares the class name of the nonexistent class.
Of course, the name of the class file is also very fastidious. It's better to have a relationship with a class, like person_class.php.