PHP - Other Overloading Methods

Introduction

PHP __isset() is called whenever the calling code attempts to call PHP's isset() function on an invisible property.

It takes one argument - the property name - and should return true if the property is deemed to be "set," and false otherwise:

Demo

<?php
        class MyClass {
              public function __isset($propertyName) {
                  return (substr($propertyName, 0, 4) =="test") ? true : false;
              }/*  w w  w.j a v a2 s  .com*/
        }
        $testObject = new MyClass;
        echo isset($testObject->banana)."\n";      // Displays""(false)
        echo isset($testObject->testBanana)."\n";  // Displays"1"(true)
?>

Result

__unset() is called when the calling code attempts to delete an invisible property with PHP's unset() function.

It shouldn't return a value, but should do whatever is necessary to "unset" the property (if applicable):

Demo

<?php
        class MyClass {

          public function __unset($propertyName) {
            echo"Unsetting property'$propertyName'\n";
          }/*from www .jav  a  2s .  c om*/
        }

        $testObject = new MyClass;
        unset($testObject->banana);  // Displays"Unsetting property'banana'"
?>

Result

__callStatic() works like __call(), except that it is called whenever an attempt is made to call an invisible static method.

Demo

<?php
        class MyClass {

          public static function __callStatic($methodName, $arguments) {
            echo"Static method'$methodName'called with the arguments: \n";
            foreach ($arguments as $arg) {
              echo"$arg \n";
            }// ww  w. j a va 2 s.  c o  m
          }
        }

        MyClass::randomMethod("apple","peach","strawberry");
?>

Result

Related Topic