Home arrow PHP arrow Page 3 - The Call Magic Function in PHP 5

Method overloading in PHP 5 with the call magic function - PHP

If you’re a PHP developer who wishes to learn how to implement and use the set of magic functions that come included with PHP 5, you’ve come to the right place. Welcome to the third part of a series that takes a close look at magic functions in PHP 5. Made up of seven tutorials, this series teaches you how to work with the most common PHP 5 magic functions, putting particular emphasis on their usage within the context of object-oriented applications.

TABLE OF CONTENTS:
  1. The Call Magic Function in PHP 5
  2. Review: the isset and unset magic functions
  3. Method overloading in PHP 5 with the call magic function
  4. The call magic method in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
June 08, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

Quite probably, one of the most helpful methods that fall under the category of magic functions is the popular "__call()." If you still haven't had the opportunity to use it with your programs, the "__call()" function is invoked behind the scenes by the PHP parser when it finds a reference to a method of a class that haven't been explicitly defined.

So, on one hand there's the pair composed by the "__set()" and "__get()" functions, which allow you to overload properties of a class, while on the other hand, "__call()" permits you to overload methods extremely easily.

Of course, this way of overloading properties and methods is only a proprietary feature of PHP 5 that differs significantly from other mature languages, such as Java and C++, but it can be really helpful when used in a creative manner.

Now that you have a more clear idea of when the "__call()" function is invoked by the PHP engine, let me show you two concrete examples, so you can spot the differences between them. In the first case, the "User" class that you saw before will be used without implementing the "__call()" method, while in the second example, the method will be implemented explicitly.

So, here's the "User" class without using method overloading:

class User

{

// constructor (not implemented)

public function _construct(){}

 

// set undeclared property in a restrictive way

public function __set($property, $value)

{

if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

{

$this->$property = $value;

}

}

 

// get declared property

public function __get($property)

{

if (isset($this->$property))

{

return $this->$property;

}

}

}

 

 

// example of usage of 'User' class with no method overloading

$user = new User();

$user->fname = 'Alejandro';

$user->lname = 'Gervasio';

$user->email = 'alejandro@mydomain.com';

// display user data

echo 'First Name : ' . $user->fetch('fname') . ' Last Name : ' . $user->fetch('lname') . 'Email : ' . $user->fetch('email');

 

/*

triggers a fatal error:

Fatal error: Call to undefined method User::fetch() in path/to/file

*/

So far, nothing unexpected is happening here, since the above script attempts to retrieve the values assigned to the properties of a fictional user via a non-existent method called "fetch()." Naturally, this makes the PHP parser raise a fatal error.

However, this issue can be addressed quickly if the same class gives a concrete implementation to the "__call()" method. Want to see how this can be done? Well, take a look at the new definition of this class, which is as follows:

class User

{

// constructor (not implemented)

public function _construct(){}

 

// set undeclared property in a restrictive way

public function __set($property, $value)

{

if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

{

$this->$property = $value;

}

}

 

// get declared property

public function __get($property)

{

if (isset($this->$property))

{

return $this->$property;

}

}

 

// single point to fetch user data

public function __call($method, $args)

{

if ($method === 'fetch' AND empty($args) === FALSE)

{

return $this->$args[0];

}

}

}

Definitely, things are getting much more interesting now. As you can see, the enhanced version of the "User" class includes the "__call()" method, which checks to see if the non-existent method being called is named "fetch()" or not. If this happens to be true, it'll return the first argument that was passed in to it.

Implementing the "__call()" function in that way will allow us to use the non-declared "fetch()" method to retrieve the values assigned to the properties of the "User" class in a really simple fashion.

But guess what? The full details of this process will be discussed in the last section of this tutorial. Therefore, to get there, click on the link below and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: