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;
              }//  www.  ja v  a 2s  . c om
        }
        $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 w  w  w. j ava2 s  .  com*/
        }

        $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";
            }//from  w  w  w.j av  a  2s  .c  o  m
          }
        }

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

Result

Related Topic