Common magic Methods in PHP summarize _php techniques

Source: Internet
Author: User
Tags class definition function prototype

Common magic Methods are: __tostring () __call () __autoload () __ Clone () __get () __set () __isset () __unset ()

1.__tostring () When you define an output object reference, the information that is often used to print some objects must have a return value
Eg: there is a persion class
Persion per =new persion ()
Echo per; Direct call can be an error
We can add the __tostring () method to the definition of the class

Copy Code code as follows:

Function __tostring ()
{
$str =this-> $name .this->age;
return $str;
}

replication of 2.__clone () objects
Reference assignment
$per 1= $per 2; And this has only one address in memory.
and $per1=clone $per 2 with two memory addresses.

the 3.__call () method automatically executes when a function that does not exist in the class instance is invoked
If you attempt to invoke a function that does not exist in the class, a syntax error occurs, in order to be able to friendly hints
We can declare the call () method in the class;
Copy Code code as follows:

Function __call ($funName, $argu)
{
Echo "named". $funName. " The parameter is ". printf ($argh)." function does not exist ",
}

4.__autoload The class file used by automatic loading the function is added on the referenced page
We've all used this situation, we need to invoke other PHP files on the page, we need to use the Include method
But if there are dozens of pages that need to be referenced, it's too cumbersome to use the AutoLoad method in this page
Copy Code code as follows:

Function __autoload ($className)
{
Include $className. ". PHP ";
}

In this way, all references to other classes are automatically referenced by the class file. The name of the class file must be the class name. php

5.__get () Accessing private properties in a class
If a property in a class is set to a private property and is inaccessible in an instance of a class, how can it be accessed?
We can use __get ()
Eg:
In the class has

Copy Code code as follows:

Class person
{
Private $name;
Private $age;
}

Instantiate person Per=new person ()
Per-> $name; This is not a value.
But if we add the __get method to the class
Copy Code code as follows:

Function __get ($proName)
{
return this-> $proName;
}

We call Per-> again $name can access the
It would be a question to ask, so that you could directly access the private variable, and what is the difference between declaring it public?
If declared as public, we can read arbitrary, if it is private, if we add a Get method, then each call to the private property will call the contents of the Get method, so that we can add some logical processing in the gets method.

6.__set () Setting private properties in a class
In principle, we can add the __set () function to the class, and every time we assign a value to a private property by invoking the class instance, the __set function is executed, the function prototype:

Copy Code code as follows:

Function __set ($proName, $value)
{
this-> $proName = $value;
}

Since it's a method assignment, we can do some logical processing

7.__isset () to determine whether a private property or method exists in a class is invoked automatically
Let's first introduce the Isset method, which is used to determine whether a property or method exists, but we cannot determine whether a private property in a class exists by using a class class instance
If we use Isset (per-> $name);//return value is False, but $name attribute does exist, how to solve it?
Workaround:
1. Define the $name as a private property
2. Add in class definition

Copy Code code as follows:

Function __isset ($proName)
{
return Isset (this-> $proName);//The inside of a class is a private property that can be accessed
}

In this case we call Isset ($name) again, and the return value is true;

8.__unset () automatically calls when a private variable in a class is cleared
In combination with the unset () Unset method, you can delete the attribute, and when we need to delete the attribute in the class, if it is a public property, we can directly
Delete, but if it's private, we can't do it with this method.
How to achieve it we can use the __unset () method to implement this feature we need to add in the class

Copy Code code as follows:

Function __unset ($proName)
{
Unset (this-> $proName);
}

Now we call unset ($name), and we can delete the private property in the person class $name

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.