Using __isset()
The __isset()
magic method is triggered by calling the isset()
or empty()
language constructs on inaccessible properties. The method accepts a single parameter, as per the following synopsis:
public bool __isset(string $name)
The $name
argument is the name of the property being interacted with.
Let's take a look at the following object context example:
<?php class User { private $data = [ 'name' => 'John', 'age' => 34, ]; public function __isset($name) { if (array_key_exists($name, $this->data)) { return true; } return false; } } $user = new User(); var_dump(isset($user->name));
The User
class defines a single protected array property called $data
, and a magic __isset()
method. The current method's inner workings simply do a name lookup against the $data
array key names and return true
if the key is found in the array, otherwise, false
. The resulting output of the example is bool(true)
.
The Magento platform provides an interesting...