PHP Class Magic Methods

Description

Whenever you see a method name start with a double underscore, it is a "magic" method. PHP reserves all methods starting with __ as magic.

Magic method list

PHP allows you to create three " magic " methods that you can use to intercept property and method accesses:

  • __get() is called whenever the calling code attempts to read an invisible property of the object
  • __set() is called whenever the calling code attempts to write to an invisible property of the object
  • __call() is called whenever the calling code attempts to call an invisible method of the object

visible refers to non-existing property or method or private or protected the property or method.

__get()

__get() specifies what to do when loading an unknown property.

For example:


<?PHP/*w ww .  jav  a2s. com*/
     class Book {
             public $Name;


             public function __get($var) {
                     print "Attempted to retrieve $var and failed...\n";
             }
     }
     $aBook = new Book;
     print $aBook->Price;
?>
Overloading Property Accesses with __get()

<?PHP/*from www . j  a v a2 s . com*/
            class Car { 
              public function __get( $propertyName ) { 
                echo "The value of '$propertyName' was requested < br / > "; 
                return "blue"; 
              } 
            } 
                  
            $car = new Car; 
            $x = $car->color; 
            echo "The car's color is $x \n";
?>

The code above generates the following result.

__set()

Use __set() method to catch an attempt to set an invisible property to a value, use needs two parameters: the property name and the value to set it to.

It does not need to return a value:


public function __set( $propertyName, $propertyValue ) { 
   // (do whatever needs to be done to set the property value) 
}      

The __set() magic method is called when setting an undefined property.


<?PHP//w  ww .ja  v  a  2 s .  c o  m
      class Book {
              public function __set($var, $val) {
                      print("UPDATE $var = '$val';");
              }

      }

      $book = new Book();
      $book ->Price= 1.2;
?>

The code above generates the following result.

__call()

Use __call() to handle calls to nonexistent methods of a class. The method should then return a value (if any) back to the calling code:


public function __call( $methodName, $arguments ) { 
   // (do stuff here) 
   return $returnVal; 
} 

The __call() magic method is called when calling a missing method. Here's an example of __call() in action:


<?PHP/*from   ww  w  . j  av  a  2  s .  c o  m*/
     class Book {
             public function __call($function, $args) {
                     $args = implode(', ', $args);
                     print "Call to $function() with args '$args' failed!\n";
             }
     }

     $aBook = new Book;
     $aBook->nonExistingMethod("foo", "bar", "baz");
?>

The code above generates the following result.

__toString()

__toString() allows you to set a string value for the object.


<?PHP/*  w w  w . j a v  a 2s  .  co m*/
     class Cat {
             public function __toString() {
                     return "This is a cat\n";
             }
     }

     $c = new Cat;
     print $c;
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition