Php _ call method instructions

Source: Internet
Author: User
Tags php file

Let's talk about the role of _ call: "A new special method added to the PHP5 object. This method is used to monitor other methods in an object. If you try to call a method that does not exist in an object, the __call method will be automatically called. "

Typical use cases:

The code is as follows: Copy code
<! --? Php
Class MethodTest {
Public function _ call ($ name, $ arguments ){
Echo "Calling object method '$ name '"
. Implode (',', $ arguments). "n ";
     }
}
$ Obj = new MethodTest;
$ Obj ---> runTest ('In object context ');

 
Running result:
Calling object method 'runtest' in object context
However, we have recently seen a _ call application in actual development. Thinkphp, as a widely used framework in China, is perfect in terms of this method!

For example, in the thinkphp: lib-> think-> core-> model. class. Php file (Model class), there is such a piece of code:

The code is as follows: Copy code
/**
+ ----------------------------------------------------------
* Use the _ call method to implement some special Model methods.
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Param string $ method name
* @ Param array $ args call parameters
+ ----------------------------------------------------------
* @ Return mixed
+ ----------------------------------------------------------
*/
Public function _ call ($ method, $ args ){
If (in_array (strtolower ($ method), array ('field', 'table', 'where', 'order', 'limit', 'page', 'Alias ', 'Having ', 'group', 'lock', 'distinct'), true )){
// Implementation of coherent operations
$ This-& gt; options [strtolower ($ method)] = $ args [0];
Return $ this;
} Elseif (in_array (strtolower ($ method), array ('count', 'sum', 'min', 'Max ', 'avg'), true )){
// Statistics Query implementation
$ Field = isset ($ args [0])? $ Args [0]: '*';
Return $ this-& gt; getField (strtoupper ($ method). '('. $ field. ') AS tp _'. $ method );
} Elseif (strtolower (substr ($ method, 0,5) = 'getby '){
// Obtain records based on a field
$ Field = parse_name (substr ($ method, 5 ));
$ Where [$ field] = $ args [0];
Return $ this-& gt; where ($ where)-& gt; find ();
} Else {
Throw_exception (_ CLASS _. ':'. $ method. L ('_ METHOD_NOT_EXIST _'));
Return;
        }
    }

I will not explain the specific functions of the code. First, I may not be able to explain it clearly. Second, I will take a look at the programming ideas in it and learn a lot.

Let's talk about his call method, and we can see that he is "powerful.
Call:
$ This-> dao = M ('table'); // Quick and high-performance instantiation of a table model
$ This-> dao-> field ($ field)-> where ($ where)-> limit ($ offset. ','. $ limit)-> select (); // Set the query field, query condition, set the query quantity, and finally perform the query operation. Of course, database records are returned.

If you see "something wrong", the field method becomes an object, and the where, limit, and select methods are also an object. In fact, the field and where methods do not exist in the Model class. Because these methods do not exist, the _ call method is executed and the $ this object is returned. Therefore, we can achieve this "cohesion" writing, and a line of code involves all the SQL statements.

I will not talk about it any more. I will introduce you to the practical application of the _ call method. If you want to fully understand it, we suggest you go to the thinkphp source code! (Declaration: I have nothing to do with thinkphp, and I am not advocating it)

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.