What is the difference between isset() and __isset()?

后端 未结 7 413
广开言路
广开言路 2020-12-07 10:59

I need to know about magic function __isset() and normal function isset(). Actually what is the real difference between php language construct

7条回答
  •  有刺的猬
    2020-12-07 11:48

    in simple words, __isset() helps isset() to work over protected/private vars in class .

    Example:

    class test
    {
        public $x = array();
    }
    

    in the above class you can do this isset($test->x['key']) as the $x is public

    but here

    class test
    {
        protected $x = array();
    
        function __isset($key)
        {
            return isset($this->x[$key]);
        }
    }
    

    $x is protected and you cannot access it, so we created __isset() to help us use isset($x['key'])

    you can say that __isset() is just a bridge for isset()

提交回复
热议问题