The __get() Method – Reading Undeclared Properties

This entry is part 2 of 4 in the series Magic Methods in PHP

Magic methods are the PHP methods that get fired in response to particular PHP events. For unique identity, the name of each magic method begins with double underscore (__). Each magic method is a member function that is available to all instances of a class.  So, before calling any magic method, it should be defined as public method within the class or scope of the program. PHP Constructors and Destructors are also two magic methods.

A constructor is a magic method that initializes certain class properties and can even trigger some class methods. A constructor magic method is automatically called at the time of an object initialization. Similarly, a destructor customizes the process of object destruction.

We will learn about useful PHP magic functions in a series of posts tagged “magic methods”.

The __get() and __set() methods are designed for working with properties that have not been declared in a class or its parent class.

__get() method

This method is invoked when the PHP code attempts to read an undeclared class property. The __get() method is called automatically with a single argument. This argument is the name of the property that the code is trying to access. The __get() can be coded to handle the response for such situations. The __get() method will return the response as if the target property exists. Let’s consider a simple example:

Class Employee {

function __get( $property ) {

$method = "get{$property}";

if(method_exists( $this, $method ) ) {

return $this->$method();

}

}

function getName() {

return "Refulz";

}

function getDesignation() {

return "Manager";

}

} 

In the above class, the __get() magic method, accepts the nonexistent property as the argument and then prefixes the word “get” with the called property name. If the method is found then the request is processed by that method.

Suppose a client attempts to access an undefined property of the Employee, then the __get() method gets invoked.

$emp = new Employee();

print $emp->designation; //Output: Manager

The above code will invoke the getDesignation() method of the Employee class.

Imagine a change of backend column in a complex application. The column name will be referred at many places, here the __get() method comes handy.

Class Employee {

public function __construct($id){

$emp = $this->getEmployee($id);

}

function getEmployee() {

// Code to read the properties from DB

// DB returns $emp->username property; instead of $emp->name

}

public function __get($field) {

if($field == 'name') {

return $this->username;

}

}

}

$emp = new Employee();

print $emp->name; // Outputs the Employee username property fetched from DB

The __get() method will fake as if the name property still exists.

Series Navigation<< The __toString() Method – Objects as StringsThe __call Method – Call Inaccessible and Non-existent Methods >>

4 Responses to “The __get() Method – Reading Undeclared Properties”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>