PHP Class Magic Methods

In this chapter you will learn:

  1. What are PHP Class Magic Methods
  2. Magic method list
  3. __get()
  4. __set()
  5. __call()
  6. __toString()

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//  j  a  v  a  2 s.c  om
     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  j  a  v  a  2 s . co  m*/
            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//from ja v a 2 s .  co 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/*  j  av a  2 s  .com*/
     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/* j  av  a2 s  .  c o m*/
     class Cat {
             public function __toString() {
                     return "This is a cat\n";
             }
     }

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

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Why do we need Query Strings
  2. Example - create query string
  3. Note for query string
  4. Example - Access data in the query string
Home » PHP Tutorial » PHP Class Definition
Concept for Object Oriented Design
PHP Class Definition
PHP Create Object from Class
PHP Class Properties
PHP Iterating Object Properties
PHP Class Inheritance
PHP Overriding Methods
PHP final Classes and Methods
PHP Abstract Class
PHP Class Access Control Modifiers
PHP Class Constructor
PHP Class Destructors
PHP Class Magic Methods