Php _ call method instructions _ PHP Tutorial

Source: Internet
Author: User
Php _ call method instructions. This article introduces how to use the php _ call method. if you need to learn the php _ call method, you will not be able to access the reference. I believe that I have read this article in reference books about PHP _ call, if you need to learn the php _ call method, do not go to the reference page. I believe that I have seen the _ call magic method in my reference books and the application examples of PHP shoes! But have you used it when you really start writing code !? To be honest, I have been using PHP for a while, but I have never applied it to actual development.

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:
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:
/**
+ ----------------------------------------------------------
* 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-> 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-> 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-> where ($ where)-> 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)

Tips _ call method instructions for users who need to learn the php _ call method. I believe that I have seen all my new PHP shoes in reference books...

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.