Overloading

Object Interfaces
November 14, 2018
Magic Methods
November 16, 2018

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. The rest of this section will use the terms “inaccessible properties” and “inaccessible methods” to refer to this combination of declaration and visibility.

All overloading methods must be defined as public.

Changelog

Version Description
5.3.0 Added __callStatic(). Added warning to enforce public visibility and non-static declaration.
5.1.0 Added __isset()and __unset(). Added support for >__get() for overloading of private properties.
5.0.0 Added __get().

Property overloading

public void __set ( string $name , mixed $value )
public mixed __get ( string $name )
public bool __isset ( string $name )
public void __unset ( string $name )

__set() is run when writing data to inaccessible properties.

__get() is utilized for reading data from inaccessible properties.

__isset() is triggered by calling isset() or empty() on inaccessible properties.

__unset() is invoked when unset() is used on inaccessible properties.

The $name argument is the name of the property being interacted with. The __set() method’s $value argument specifies the value the $name‘ed property should be set to.

Property overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods should not be declared static. As of PHP 5.3.0, a warning is issued if one of the magic overloading methods is declared static.

Example #1 Overloading properties via the __get(), __set(), __isset() and__unset() methods

<?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”;
?>

The above example will output:

Setting 'a' to '1'
Getting 'a'
1

Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)

1

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'


Notice:  Undefined property via __get(): hidden in <file> on line 70 in <file> on line 29
Sondiva Technologies
Sondiva Technologies
Sondiva Technologies started in 2011 with a vision of providing Quality based Web Hosting and Web Designing Services in Ghaziabad, Delhi NCR. Backed up by full stack developers and designers , with over 500+ Web Projects. They work on opensource Platforms like Wordpress, Magneto, Prestashop, Opencart and Core PHP. Other services are : Responsive Web Designing Domain Registration Services Dedicated & SSD Hosting Virtual Private Servers Google GSuite Mail Servers E-Commerce Shopping Portal Development Web Designing & Application Courses SEO & Digital Marketing

Leave a Reply

Your email address will not be published. Required fields are marked *

Shop Now