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

I need to know about the magic function __isset() and the normal function isset() . Actually, what is the real difference between php isset() language construct and php __isset() magic method? When I google, they said that __isset() is a magic function. What is the difference between common php functions and magic functions in php?

+54
php
Jan 20 '14 at 6:31
source share
7 answers

isset()

This 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()

This is a magic method that is called when isset() or empty() checks for a nonexistent or inaccessible property of the class:

 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 *** 
Result

__isset() will be automatically added as bool anyway.

You can actually define a custom function __isset() , but it has nothing to do with the magic method.

See this example .




Magic methods

Unlike general functions, it is possible to define only in the scope of a class and automatically activate certain events, such as: inaccessible method call, serialization of classes when unset() used for inaccessible properties, etc. See also this white paper: Overloading .

+38
Jan 20 '14 at 7:20
source share

__ isset is a magic method. Magical methods are methods called internally.

Consider the 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 a magic method, but you will not call it. When the string is echo $ class; performed. PHP knows that now I have to consider the $ class object as a string and consider any object as a string_toString method, if implemented in this class.

All magic methods are called indirectly.

Another example following

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

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

+5
Jan 20 '14 at 6:48
source share

First of all, let me tell you what isset () does. The isset () function checks if a value is set or whether it is null. The _isset () function is a magic method in PHP. Any function with "_" at the beginning is a magic method in PHP. Now __isset () is called by isset () or empty () in inaccessible properties, this means those properties that were not defined in the class and explicitly defined at run time. Here is a piece of code that should help you better understand:

 <?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 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"; ?> 
+5
Jan 22 '14 at 11:12
source share

in simple terms, __ isset () helps isset () work on protected / private vars in the class .

Example:

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

in the above class you can do this isset($test->x['key']) since $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 to isset()

+4
Jan 23 '14 at 12:07
source share

Magic functions are automatically called (triggered) when something happens. Normal functions must be specifically called by your PHP code.

In your case: __isset () will be called automatically if you have isset () that is trying to get an unavailable 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 
+1
Jan 24 '14 at 17:58
source share

What is the difference between regular php functions and magic functions in php?

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

Determine if a variable is set and is not NULL

But __isset () is an overload of class properties .

PHP overloading provides the means to dynamically "create" properties and methods. These dynamic objects are processed using magic methods that can be set in the class for various types of actions. Overload methods are called when interacting with properties or methods that were not declared or not visible in the current area.

It will be called magically behind the scene, as described above, if announced in class. Let the experiment overload the property of the PHP class.

 <?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 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 outputs

  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 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 that the "hidden" property is not set and shows bool(false) , but returns a value of "2" later, because the "hidden" property does not appear outside the class and calls the magic __isset() function, but it is also not set in 'data', therefore returns bool(false) . In the getHidden() function, although it returns a private property of the object that is visible to the internal objects of the object. In the last var_dump($obj->hidden) it calls the __get() method and returns NULL. Because in the __get() method, it is looking for data['hidden'] , which is NULL .

Note: here is an example from PHP Manuel: Overloading with some changes.

Hope this helps!

+1
Oct 29 '17 at 21:45
source share

isset() for variables, and __isset() for class properties.

-one
Apr 14 '14 at 14:23
source share



All Articles