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

Solution 1:

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.

Solution 2:

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