Various interceptors in the php class

Source: Internet
Author: User
1. When _ get ($ property) accesses an undefined property, it calls classlanjie {function _ get ($ name) {echo $ name. propertynotfound !;}} $ Obnewlanjie (); echo $ ob-g; when we call the $ ob undefined attribute g, call the interceptor _ get () method and output "gpropertynotfound !";

1. When _ get ($ property) accesses an undefined property, class lanjie {function _ get ($ name) {echo $ name. property not found is called! ;}}$ Ob = new lanjie (); echo $ ob-g; when we call the $ ob undefined attribute g, we call the interceptor _ get () method, output "g property not found! ";

1. It is called when _ get ($ property) accesses an undefined property.



class lanjie{    function __get($name)    {        echo $name." property not found! ";    }}$ob = new lanjie();echo $ob->g;



When we call the $ ob undefined attribute g of the object, call the interceptor _ get () method and output "g property not found !";


2. _ set ($ property, $ value) assigns a value to an undefined property call.



class person{    private $_age;    private $_name;    function __set($name, $value)    {        $method = "set".  ucfirst($name);        echo $method;        if(method_exists($this, $method) )        {            return $this->$method( $value );        }    }        function setName( $name )    {        $this->_name = $name;        if( !is_null($this->_name) )        {            $this->_name = strtoupper($this->_name);        }    }    function setAge( $age )    {        return $this->_age = (int)$age;    }}$p = new person();$p->name = 'bob';print_r( array( $p ) );


We can clearly see that when an undefined 'name' value is assigned, "_ set ()" is called ()"


Others include _ call (), _ isset (), and _ unset ();

The most useful and commonly used here is _ call (), which is called when an existing method is called; _ isset () is called when an isset () function is used for a defined attribute, and _ unset is called when unset is used for unset numbers.

Http://blog.csdn.net/shuiping567541/article/details/7061258

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.