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.
blog comments powered by Disqus |
|
|
|
|
|
|
|