Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

like image 295
Lal krishnan S L Avatar asked Jan 20 '14 06:01

Lal krishnan S L


People also ask

What is an isset () function?

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

Does Isset check for empty?

The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL. The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.

Why is Isset used in PHP?

The isset function in PHP is used to determine whether a variable is set or not. A variable is considered as a set variable if it has a value other than NULL. In other words, you can also say that the isset function is used to determine whether you have used a variable in your code or not before.

Is set array PHP?

array is not set. This is also a predefined function in PHP which checks whether an index or a particular key exists in an array or not. It does not evaluate the value of the key for any null values. It returns false if it does not find the key in the array and true in all other possible cases.


2 Answers

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.

like image 69
17 revs, 2 users 92% Avatar answered Oct 14 '22 12:10

17 revs, 2 users 92%


__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.

like image 41
Hassan Avatar answered Oct 14 '22 14:10

Hassan