_php tutorial based on PHP5 Magic constants and Magic methods

Source: Internet
Author: User
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 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 (case-sensitive) when the function is defined. In PHP4, the value is always lowercase.
4. __class__
Returns the name of the class (PHP4.3.0 new addition). Since PHP5 this constant returns the name (case-sensitive) when the class is defined. In PHP4, the value is always lowercase.
5. __method__
Returns the method name of the class (PHP5.0.0). Returns the name of the method when it is defined (case-sensitive).
Magic Function:
1. __construct ()
Constructor: Called when an object is instantiated,
When a __construct and a constructor with a class name are present, __construct is called and the other is not called.
4. __get ()
When a property of an object is read, the property value is returned directly if the property exists, 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 ()
Called when an object is printed. 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 called. If the object is relatively large, want to cut a bit of the east and then serialize, you can consider this function.
9. __wakeup ()
Unserialize is called to do some initialization of the object.
10. __isset ()
Called when detecting 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 called. Use the return value of __set_state as the return value of Var_export.
13. __autoload ()
When an object is instantiated, the method is called if the corresponding class does not exist.

The method of first Knowledge magic
Php5.0 has provided us with a lot of object-oriented features since its release, especially for our easy-to-use magic methods that allow us to simplify our coding and better design our systems. Today we will 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. "
”.” Age: ". $this->age."
”;
}
}
Now I'm going to instantiate this class and then print this example:
$p 1= New Person ("Liuzy", 20);
echo $p 1; Direct printing will make an error
It is obvious that this is an error to print directly to the object, 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. "
”;
}
then refresh the page and find out what?
Now we understand that __tostring () is the method that executes when the object is printed directly, and we can use this method to print some information about the class.Note: There are two underscores, and 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 do:
$p 1->say ();
$p 2->say ();
Can be executed, and the effect is the same.

We also have a way to:
$p 3= clone $p 1; Note that clone is the Clone keyword, and 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: The $this here is the clone-generated object itself, not the current class
}
Then we execute:
$p 3->say ();
Print out:
Name: I am a copy
Age:20
Here we understand that the __clone () method is the method that executes when the object is cloned, and it acts as a property initialization for the newly cloned copy.
__call ()
The main function of this method is to execute the __call () method when an instance of the class calls a non-existent method. Note that you need to declare in the class in advance:
Function__call ($fname, $argus)
{
echo "The method you called:". $fname. " does not exist
”;
echo "parameter is". Print_r ($argus);
}
__autoload ()
When we call a class in peacetime, we must first introduce the file (include "xxx.php"), if we call a lot of classes in a page, then we have to use a lot of include "xxx.php". This is obviously a hassle.
The __autoload () method can help us solve this problem.

For example, we define the file that contains the person class above as person_class.php,
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 way the test.php page is executed without errors.
The __autoload () method is a method that is called when a class does not exist in life, and it has a string that declares the class name for the nonexistent class.
Of course, the name of the class file is also very fastidious. It's better to have a relationship with the class, like person_class.php.

http://www.bkjia.com/PHPjc/327629.html www.bkjia.com true http://www.bkjia.com/PHPjc/327629.html techarticle Magic constant: 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. Self-ph ...

  • 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.