A little discovery of the PHP magic method __call

Source: Internet
Author: User

Long time no Blog park to write articles, tonight finally a little empty, just to write about yesterday's little discovery.

As far as I know, C++,java's object-oriented features are polymorphic, but PHP does not, but PHP can implement polymorphic properties by rewriting the inheritance chain method. And the Magic method is triggered in certain situations, and we can rewrite it.

---------------------------------------------------------------------------------------------------------Excerpt starts------------ ------------------------------------------------------------------------------------------

The following excerpt from 5idev.com:

__call () (Method overloading)

To avoid errors when the calling method does not exist, you can use the __call () method to avoid it. The method will be called automatically when the calling method does not exist, and the program will continue to execute.

1 function __call (string$function _namearray$arguments)2  {3...     4 }

The method has two parameters, the first parameter $function _name automatically receives the method name that does not exist, and the second $args receives multiple arguments of the non-existent method in the form of an array.

---------------------------------------------------------------------------------------------------------Excerpt ends------------ ------------------------------------------------------------------------------------------

With this feature, and PHP's function Call_user_func_array, you can achieve the effect of calling a method in a non-inheritance chain with the current object in the controller, and I want to call the Redis method (because $this is more than $this-> Redis A lot of dozen words ... And think PHP inside the cache expansion is also very cumbersome to use (Lll¬ω¬))

Paste the following code:

1<?PHP2 namespace Home\controller;3  UseThink\controller;4 classBasecontrollerextendsController {5 6     protected $redis=NULL;7     /**8 * [__construct description]9 * Instantiation of RedisTen      */ One      Public function__construct () { A         if(!$this-Redis) { -             $this->redis =New\home\controller\rediscontroller (); -         } theParent::__construct (); -     } -  -     /** + * Override method overloads for easy use of Redis - * @param string $method _name overloaded method name + * @param array $args parameters A * @return Mixed return results at      */ -      Public function__call ($method _name,$args){ -         if(method_exists($this->redis,$method _name)) { -             return Call_user_func_array(Array($this->redis,$method _name),$args); -         } -Parent::__call (); in     } -}

After writing, I really feel that I am particularly coquettish! But because it was in think, PHP3.2.3 framework inside, get method can not call Redis (but in fact, Redis or controller class inside the method renamed) Although do not know whether this will have any hidden trouble or problem, but I think this is still very convenient, later there are other classes, you can add in.

If the code has any shortcomings or errors, please correct me ~ Thank you ~

A little discovery of the PHP magic method __call

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.