How to implement various interceptor ideas in PHP

Source: Internet
Author: User
PHP provides several interceptors that are called when accessing undefined methods and properties, and __get ($property) is called when an undefined class property is accessed, $property the name of the property being accessed. When an undefined class property is assigned a value, __set ($property, $value) is called, $property is the assigned property name, $value the assigned value.

First download the various interceptor libraries in PHP that we need to use for this lesson: http://www.php.cn/xiazai/leiku/625

After the download, find the PHP class files we need, unzip to our local directory, create a new PHP file!

Once this is done, we will invoke this class in the new PHP file, and instantiate the class:

<?phpinclude_once "dingyi.php";//Introduce class file $ob = new Lanjie ();       Instantiate//When we call the object $ob undefined property g, call the Interceptor __get () method, Output "G Property not found!" echo $ob->g;echo "<br>"; $p = new Person (); Instantiate $p->name = ' bob ';//Here we can clearly see that when assigning a value to an undefined ' name ',  the "__set ()" Print_r (Array ($p)) is called; >

Run this file and get the results as shown:

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.