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

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

    What are difference between common php functions and magic functions in php?

    Common PHP function are declared and accessible with expected inputs and results but they should be called. In contrast, magic functions are defined in PHP but when defined in a class then they will be called automatically. For example the isset() is a PHP function

    Determine if a variable is set and is not NULL

    But __isset() is a Property overloading of a class.

    Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types. The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.

    It will be called magically behind the scene as described above if declared in the class. Let's experiment the PHP Class Property overloading.

    <?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;
    
            //__set() is called when 'a' property is not visible outside of class
            $obj->a = 1;
            //__get() is called when 'a' property is not visible outside of class
            echo "a: ".$obj->a . "\n\n";
            //__isset() is called when 'a' property is not visible outside of class
            var_dump(isset($obj->a));
            unset($obj->a);
            //__isset() is called when 'a' property is not visible outside of class
            var_dump(isset($obj->a));
            echo "\n";
            //__isset() is not called as 'declared' property is visible outside of class
            var_dump(isset($obj->declared));
            //__get() is not called as 'declared' property is visible outside of class            
            echo "declared: ". $obj->declared . "\n\n";
            //__set() is not called as 'declared' property is visible outside of class
            $obj->declared = 3;
            //__get() is not called as 'declared' property is visible outside of class 
            echo "declared: ". $obj->declared . "\n\n";
    
            //__isset() is called as 'hidden' property is not visible outside of class 
            var_dump(isset($obj->hidden));
    
            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";
            var_dump($obj->hidden);
    
     ?>
    

    The above code will output

        Setting 'a' to '1'
        Getting 'a'
        a: 1
    
        Is 'a' set?
        bool(true)
        Unsetting 'a'
        Is 'a' set?
        bool(false)
    
        bool(true)
        declared: 1
    
        declared: 3
    
        Is 'hidden' set?
        bool(false)
        Let's experiment with the private property named 'hidden':
        Privates are visible inside the class, so __get() not used...
        2
        Privates not visible outside of class, so __get() is used...
        Getting 'hidden'
        NULL
    

    It says the 'hidden' property is not set and shows bool(false) but echos out value '2' later because the 'hidden' property is not visible outside of the class and it calls __isset() magic function but it also is not set in 'data' so it returns bool(false). In getHidden() function though it returns the object private property 'hidden' which is visible to object internal functions. In last var_dump($obj->hidden) it calls __get() method and it returns NULL. Because in __get() method it looks for data['hidden'] which is NULL.

    Note: the example here is from PHP Manuel: Overloading with some modifications.

    Hope this helps!

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