Detailed explanation of magic constants and magic methods based on PHP5

  • 2020-06-15 07:51:23
  • OfStack

Magic constants:
1. __LINE__
Returns the current line number in the file.
2. __FILE__
Returns the full path and file name of the file. Returns the include file name if used in the include file. Starting with PHP4.0.2, ___ always contains 1 absolute path, and successive versions sometimes contain 1 relative path.
3. __FUNCTION__
Returns the function name (es12EN4.3.0 added). This constant since PHP5 returns the name of the function when it was defined (case sensitive). In PHP4 this value is always lowercase.
4. __CLASS__
Returns the name of the class (es18EN4.3.0 added). This constant since PHP5 returns the name of the class when it was defined (case sensitive). In PHP4 this value is always lowercase.
5. __METHOD__
Returns the method name of the class (PHP5.0.0 added). Returns the name of the method when it was defined (case sensitive).
Magic function:
1. __construct ()
Constructor: called when instantiating an object,
Each ___ 30en is called with a constructor with the class name of the function, and the other one is not called.
4. __get ()
When reading the property of an object, if the property exists, it will directly return the property value; If it doesn't exist, the ___ 35en function is called.
5. __set ()
When you set the properties of an object,
If the attribute exists, assign it directly;
If not, the ___ function is called.
6. __toString ()
Is called when 1 object is printed. Such as echo $obj; Or print $obj;
7. __clone ()
Called when the object is cloned. Such as: $t = newTest (); $$t t1 = clone;
8. __sleep ()
serialize was called before. If the object is large and you want to subtract 1 thing from the serialization, consider this function.
9. __wakeup ()
unserialize is called to do some object initialization.
10. __isset ()
Is called to check if an object property exists. Such as: isset ($c - > name).
11. __unset ()
unset1 object properties are called. Such as: unset ($c - > name).
12. __set_state ()
Called when var_export is called. Return the return value of ___ 86EN_ES87en with the return value of ___.
13. __autoload ()
When instantiating an object, the method is called if the corresponding class does not exist.

First knowledge of magic methods
Php5.0 has given us a lot of object-oriented features since its release, and in particular has given us a lot of easy-to-use magic methods that allow us to simplify our coding and design our systems better. Today we are going to learn the magic tricks from php5.0.
PHP | | magic methods __toString (), __clone (), __call (), __autoload () explanation
__toString()
If I have 1 class:
classPerson
{
private $name = "";
private $age = 0;
___ ($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 I'm going to print this instance:
$p1 = new person (" liuzy ", 20);
echo $p1; // It's wrong to print directly
Obviously, it would be an error to print the object directly because the object is a reference handle and cannot be printed directly. Let's use the ___ 167en () method. Let's add 1 ___ in the Person class with the method:
function__toString()
{
return "I am Person,my name is" > name." < br/ > ";
}
Then refresh the page. What do you find?
Let's see, ___ 192en () is the method to be executed with the direct printing of the object, with which we can print some information about the class. Note: two underscores. The method must have a return value.
__clone()
We know that objects can be assigned directly, for example
$p2 = $p1; So this is 1 object with 2 references
Then I execute:
$p1- > say();
$p2- > say();
Can be executed, and the effect of 1.

Here's another way:
$$p1 p3 = clone; // Note that clone is the clone keyword, here the difference is $p3 is a new object.
At the same time, we add a method to the class:
function__clone()
{
$this- > name = "I am the copy"; // Note: $this is the cloned object itself, not the current class
}
Then we execute:
$p3- > say();
Print out:
name: Copy
age:20
By this point, we understand that the method with ___ 240en () is executed when the object is cloned, with operations such as attribute initialization on the newly cloned copy.
__call()
The main function of this method is to execute the method with each class instance calling a nonexistent method. Note that you need to declare in the class in advance:
function__call($fname,$argus)
{
$fname does not exist < br/ > ";
print_r($argus);
}
__autoload()
When we call a class, we have to introduce the file (include "xxx.php"). If we call many classes on one page, we have to use a lot of include "xxx.php". Obviously it's a hassle.
___, 272en () can help us with this problem.

For example, we define the file containing the Person class above as ES276en_ES277en.php,
Create another php file test. php and edit:
function __autoload($calssName)
{
include $className. "_class. php"; // Perhaps you will understand when you see this? Ha ha
}
$p = new Person (" mifan ", 22);
$p- > say();
The test.php page is executed without an error.
The method with succautoload () is called if a class is not alive and has 1 argument of type string as the name of the class declaring the non-existent class.
Of course, class file naming is also very fastidious. Preferably with a class, such as ES310en_class.php


Related articles: