PHP Magic Method

Source: Internet
Author: User
<span id="Label3"></p><p>PHP Magic Method:<br>Summary of Magic methods in Php: __construct, __destruct, __call,<br>__callstatic,__get, __set, __isset, __unset, __sleep,<br>__wakeup, __tostring, __set_state, __clone and __autoload<br>1.__tostring () is used to define output object references when calling information that is commonly used to print some objects must have a return value<br>Eg: there is a persion class<br>Persion per =new persion ()<br>Echo per; Direct call will go wrong<br>We can add the __tostring () method to the definition of the class<br>Function __tostring ()<br>{<br>$str =this-> $name .this->age;<br>Return $str;<br>}<br>__autoload automatically loads the class file used by the function is added on the referenced page<br>We have used this situation, we need to call other PHP files on the page, we need to use the Include method<br>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<br>Function __autoload ($className)<br>{<br>Include $className. ". PHP ";<br>}<br>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<br>__set () setting private properties in a class<br>In the same 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, we execute the __set function, the function prototype:<br>Function __set ($proName, $value)<br>{<br>this-> $proName = $value;<br>}<br>Since it is a method assignment, we can do some logical processing<br>__isset () automatically called when a private property or method exists in a class<br>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.<br>If we use Isset (per-> $name);//the return value is false, but the $name property does exist, how to solve it?<br>Workaround:<br>1. Define $NAME as a private property<br>2.<br>Adding in the class definition<br>Function __isset ($proName)<br>{<br>Return isset (this-> $proName);//re-class internal access to the private property<br>}<br>In this case we call Isset again ($name); The return value is true;<br>__unset () automatically called when a private variable in a class is cleared<br>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<br>delete, But if it's private, we can't do it by this method.<br>How do we do that? we can use the __unset () method to implement this function we need to add the<br>Function __unset ($proName)<br>{<br>Unset (this-> $proName);<br>}<br>Now we call unset ($name) and we can delete the private attribute in the person class $name</p><p><p>PHP Magic Method</p></p></span>

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.