© Mikael Olsson 2021
M. OlssonPHP 8 Quick Scripting Referencehttps://doi.org/10.1007/978-1-4842-6619-9_21

21. Overloading

Mikael Olsson1  
(1)
Hammarland, Finland
 

Overloading in PHP provides the ability to add object members at run-time. This is done by having the class implement the overloading methods __get, __set, __call, and __callStatic. Bear in mind that the meaning of overloading in PHP is different from many other languages.

Property Overloading

The __get and __set methods provide a convenient way to implement getter and setter methods, which are methods that are often used to safely read and write to properties. These overloading methods are invoked when using properties that are inaccessible, either because they are not defined in the class or because they are unavailable from the current scope. In the following example, the __set method adds any inaccessible properties to the $data array, and __get safely retrieves the elements from this array.
class MyProperties
{
  private $data = array();
  public function __set($name, $value)
  {
    $this->data[$name] = $value;
  }
  public function __get($name)
  {
    if (array_key_exists($name, $this->data))
      return $this->data[$name];
  }
}
When setting the value of an inaccessible property, __set is called with the name of the property and the value as its arguments. Similarly, when accessing an inaccessible property, __get is called with the property name as its argument.
$obj = new MyProperties();
$obj->a = 1;  // __set called
echo $obj->a; // __get called

Method Overloading

There are two methods for handling calls to inaccessible methods of a class: __call and __callStatic. The __call method is invoked for instance method calls.
class MyClass
{
  public function __call($name, $args)
  {
    echo "Calling $name $args[0]";
  }
}
// "Calling myTest in object context"
(new MyClass())->myTest('in object context');
The first argument to __call is the name of the method being called and the second is a numeric array containing the parameters passed to the method. These arguments are the same for the __callStatic method, which handles calls to inaccessible static methods.
class MyClass
{
  public static function __callStatic($name, $args)
  {
    echo "Calling $name $args[0]";
  }
}
// "Calling myTest in static context"
MyClass::myTest('in static context');

Isset and unset Overloading

The built-in constructs isset, empty, and unset only work on explicitly defined properties, not overloaded ones. This functionality can be added to a class by overloading the __isset and __unset methods .
class MyClass
{
  private $data = array();
  public function __set($name, $value) {
    $this->data[$name] = $value;
  }
  public function __get($name) {
    if (array_key_exists($name, $this->data))
      return $this->data[$name];
  }
  public function __isset($name) {
    return isset($this->data[$name]);
  }
  public function __unset($name) {
    unset( $this->data[$name] );
  }
}
The __isset method is invoked when isset is called on an inaccessible property.
$obj = new MyClass();
$obj->name = "Joe";
isset($obj->name); // true
isset($obj->age);  // false
When unset is called on an inaccessible property, the __unset method handles that call.
unset($obj->name); // delete property
isset($obj->name); // false
The empty construct only works on overloaded properties if both __isset and __get are implemented. If the result from __isset is false, the empty construct returns true. If, on the other hand, __isset returns true, then empty retrieves the property with __get and evaluates if it has a value considered to be empty.
empty($obj->name); // false
empty($obj->age);  // true
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.41.187