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

后端 未结 7 414
广开言路
广开言路 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:36

    Magic functions are automatically invoked (triggered) when something happens. Normal functions have to be specifically invoked by your php code.

    In your case: __isset() will be automatically invoked when you have an isset() which is trying to get a non-accessible property.

    Example:

    root@folgore:/tmp/php# cat a.php 
    <?php
    class a {
        private $att1;
        public $att2;
    
        function __isset($field) {
            echo "__isset invoked for $field\n";
        }
    }
    
    $obj=new a();
    
    // __isset will be triggered:
    isset($obj->att1);
    
    // __isset will not be triggered:
    isset($obj->att2);
    
    root@folgore:/tmp/php# php a.php 
    __isset invoked for att1
    
    0 讨论(0)
  • 2020-12-07 11:44

    First of all let me tell you what isset() function does. The isset() function checks whether the value has been set or is it null. The _isset() function is a magic method in PHP. Any function with a " _ " in the beginning is a magic method in PHP. Now, __isset() is invoked by calling isset() or empty() on inaccessible properties, by that I mean those properties that have not been defined in the class and are being explicitly defined at the run time. Here is a piece of code that should make you understand it better:

    <?php
    class PropertyTest
    {
        /**  Location for overloaded data.  */
        private $data = array();
    
        /**  Overloading not used on declared properties.  */
        public $declared = 1;
    
        /**  Overloading only used on this when accessed outside the class.  */
        private $hidden = 2;
    
        public function __set($name, $value)
        {
            echo "Setting '$name' to '$value'\n";
            $this->data[$name] = $value;
        }
    
        public function __get($name)
        {
            echo "Getting '$name'\n";
            if (array_key_exists($name, $this->data)) {
                return $this->data[$name];
            }
    
            $trace = debug_backtrace();
            trigger_error(
                'Undefined property via __get(): ' . $name .
                ' in ' . $trace[0]['file'] .
                ' on line ' . $trace[0]['line'],
                E_USER_NOTICE);
            return null;
        }
    
        /**  As of PHP 5.1.0  */
        public function __isset($name)
        {
            echo "Is '$name' set?\n";
            return isset($this->data[$name]);
        }
    
        /**  As of PHP 5.1.0  */
        public function __unset($name)
        {
            echo "Unsetting '$name'\n";
            unset($this->data[$name]);
        }
    
        /**  Not a magic method, just here for example.  */
        public function getHidden()
        {
            return $this->hidden;
        }
    }
    
    
    echo "<pre>\n";
    
    $obj = new PropertyTest;
    
    $obj->a = 1;
    echo $obj->a . "\n\n";
    
    var_dump(isset($obj->a));
    unset($obj->a);
    var_dump(isset($obj->a));
    echo "\n";
    
    echo $obj->declared . "\n\n";
    
    echo "Let's experiment with the private property named 'hidden':\n";
    echo "Privates are visible inside the class, so __get() not used...\n";
    echo $obj->getHidden() . "\n";
    echo "Privates not visible outside of class, so __get() is used...\n";
    echo $obj->hidden . "\n";
    ?>
    
    0 讨论(0)
  • 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()

    0 讨论(0)
  • 2020-12-07 11:51

    __isset is the magic method. Magic methods are the methods called internally.

    Consider following code

    <?php
    // Declare a simple class
    class TestClass
    {
        public $foo;
    
        public function __construct($foo)
        {
            $this->foo = $foo;
        }
    
        public function __toString()
        {
            return $this->foo;
        }
    }
    
    $class = new TestClass('Hello');
    echo $class;
    ?>
    

    here _toString is magic method but you will not call that. When the line echo $class; is executed. PHP knows that now I should treat $class object as a string and to treat any object as the string call_toString method, if implemented in that class.

    All magic methods called like this in indirect way.

    Another example as follow

    <?php
    class CallableClass
    {
        public function __invoke($x)
        {
            var_dump($x);
        }
    }
    $obj = new CallableClass;
    $obj(5);
    var_dump(is_callable($obj));
    ?>
    

    Similarly, in above code , var_dump(is_callable($obj)); invokes __invoke magic method indirectly.

    0 讨论(0)
  • 2020-12-07 11:54

    isset() is for variables and __isset() is for properties of a class.

    0 讨论(0)
  • 2020-12-07 11:57

    isset()

    It is a language construct that checks the initialization of variables or class properties:

    $a = 10;
    
    isset($a);     // true
    isset($a, $b); // false
    
    class Test
    {
        public $prop = 10;
    }
    
    $obj = new Test;
    isset($obj->prop); // true
    

    __isset()

    It is a magic method that is invoked when isset() or empty() check non-existent or inaccessible class property:

    class Test
    {
        public function __isset($name) {
            echo "Non-existent property '$name'";
        }
    }
    
    $obj = new Test;
    isset($obj->prop); // prints "Non-existent property 'prop'" and return false
    

    Difference:

               isset()                               __isset()
    
    Language construct                    | Magic method
                                          |
    Always return bool                    | Result depends on custom logic*
                                          |
    Must be invoked in code               | Called automatically by event
                                          |
    Unlimited number of parameters        | Has only one parameter
                                          |
    Can be used in any scope              | Must be defined as method**
                                          |
    Is a reserved keyword                 | Not a reserved keyword
                                          |
    Can't be redefined (Parse error)      | Can be redefined in extended class***

    __isset() result anyway will be automatically casted as bool.

    Actually you can define custom function __isset() but it has nothing to do with the magic method.

    See this example.


    Magic Methods

    Unlike common functions can be defined only in class scope and invoked automatically on specific events such as: inaccessible method invocation, class serialization, when unset() used on inaccessible properties and so on. See also this official documentation: Overloading.

    0 讨论(0)
提交回复
热议问题