A summary of several magic method syntax and usage examples in PHP

Source: Internet
Author: User
Tags autoload function prototype
Common magic Methods are: Tostring () call () AutoLoad () Clone () GET () SET () isset () unset ()

1.Tostring () is used to define the output object reference when calling the information that is commonly used to print some objects must have a return value
Eg: there is a persion class
Persion per =new persion ()
Echo per; Direct call will go wrong
We can add the ToString () method to the definition of the class

Function  Tostring () {$str =this-> $name .this->age; Return $str;}

2.clone () Copying of 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 executes automatically when a function that does not exist in the class instance is called
If you try to invoke a function that does not exist in the class, a syntax error occurs, in order to be friendly hints
We can declare the call () method in the class;

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

4.autoLoad automatically load the class file used by the function is added on the referenced page
We have used this situation, we need to call other PHP files on the page, we need to use the Include method
However, if there are dozens of pages that need to be referenced, it is too cumbersome, we can use the AutoLoad method in this page

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

In this way, any reference to other classes will automatically refer to the class file. The name of the prerequisite 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, it is inaccessible in an instance of the class, but how can it be accessed?
We can use Get ()
Eg:
class that has

Class Person{private $name; Private $age;}

Instantiate person Per=new person ()
Per-> $name; This is not a value.
But if we add a get method to the class

Function GET ($proName) {Return this-> $proName;}

We call Per-> again $name we have access to the
This will ask questions, so that you can directly access the private variables, and declared as public what is the difference?
If it is declared as public, we can read it arbitrarily, and if it is private, if we add a Get method, the contents of the Get method will be called each time the private property is called, so that we can add some logic to the GET method.

6.SET () Setting private properties in a class
In the same principle, we can add the set () function to the class, and whenever we assign a value to a private property by invoking a class instance, we execute the SET function, the function prototype:

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

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

7.isset () automatically called if a private property or method exists in the class
First we introduce the Isset method, which is used to determine whether a property or method exists, but we cannot judge whether a private property exists in a class through a class class instance.
If we use Isset (per-> $name);//The return value is false, but the $name property does exist, how to solve it?
Workaround:
1. Define $name as a private property
2. Adding in the class definition

Function Isset ($proName) {Return  isset (this-> $proName);//re-class internal can access the private properties of the}

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

8.unset () automatically called when a private variable in a class is cleared
In combination with the unset () unset method can delete the attribute, when we need to delete the property in the class, if it is a public property we can directly
Delete, but if it's private, we can't do it by this method.
How do we do that? We can use the unset () method to implement this function we need to add the

Function unset ($proName) {unset (this-> $proName);}

Now we call unset ($name) and we can delete the private attribute 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.