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?
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.
__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.
First of all let me tell you what isset() function does. The isset() function checks whether the value has been set or is it null. The _isset() function is a magic method in PHP. Any function with a " _ " in the beginning is a magic method in PHP. Now, __isset() is invoked by calling isset() or empty() on inaccessible properties, by that I mean those properties that have not been defined in the class and are being explicitly defined at the run time. Here is a piece of code that should make you understand it better:
<?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'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";
echo $obj->hidden . "\n";
?>
in simple words, __isset() helps isset() to work over protected/private vars in class .
Example:
class test
{
public $x = array();
}
in the above class you can do this isset($test->x['key'])
as the $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 for isset()
isset()
result to bool
because it always return bool
. –
Dangle Magic functions are automatically invoked (triggered) when something happens. Normal functions have to be specifically invoked by your php code.
In your case: __isset() will be automatically invoked when you have an isset() which is trying to get a non-accessible 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
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!
isset()
is for variables and __isset()
is for properties of a class.
© 2022 - 2024 — McMap. All rights reserved.
__isset
by any chance?_isset
does not exist natively. If yes, first hit on Google: php.net/manual/en/language.oop5.overloading.php "__isset() is triggered by calling isset() or empty() on inaccessible properties." – Queridasisset()
and_isset()
?? – Dyslaliaisset()
is not a function. It's a language construct. – Bowler