This article to introduce the PHP __call method using instructions, there is a need to learn PHP __call method of Friends do not enter the reference. I believe that the first contact with PHP children's shoes, in the reference books have seen __call this magical method, but also see its application examples! But, when you really started to write code, you used it!? To tell the truth, I have been using PHP for some time, but I really never applied it to actual development.
Let's talk about the role of __call: "The PHP5 object has a new dedicated method for monitoring other methods in an object." If you try to invoke a method that does not exist in an object, the __call method will be called automatically. "
Typical Use cases:
| The code is as follows |
Copy Code |
Runtest (' in object context ');
|
Operation Result:
Calling object method ' Runtest ' in object context
However, recently saw a __call in the actual development of the application. Thinkphp as a common domestic application of the framework, in which the application of the method of the individual feel that the calculation is perfect ah!
For example, in the thinkphp: lib->think->core->model.class.php file (model Class) there is such a code:
| The code is as follows |
Copy Code |
/** +---------------------------------------------------------- * Using __call method to realize some special model methods +---------------------------------------------------------- * @access Public +---------------------------------------------------------- * @param string $method 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) { The realization of coherent operation $this->options[strtolower ($method)] = $args [0]; return $this; }elseif (In_array (Strtolower ($method), Array (' count ', ' Sum ', ' min ', ' Max ', ' avg '), true)) { The implementation of statistical query $field = isset ($args [0])? $args [0]: ' * '; return $this->getfield (Strtoupper ($method). ' ('. $field. ') As Tp_ '. $method); }elseif (Strtolower (substr ($method, 0,5)) = = ' Getby ') { Get 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 } }
|
The specific function of the code, I do not explain, the first may I also explain not clear; second, I go to see the programming ideas inside there are many values of learning.
By talking about his calling method, he can see his "strong".
Call:
$this->dao= M (' table '); Fast, high-performance instantiation of a table-based model
$this->dao->field ($field)->where ($where)->limit ($offset. ',' . $limit)->select (); Set query fields, query criteria, set the number of queries, and finally perform query operations. Of course, the database records are returned.
See "Something wrong", the field method is an object, where, limit, select method is also an object, in fact, field, where these methods in the model class does not exist. Because these methods do not exist, the __call method is executed at this time, and then the $this object is returned. So you can implement this kind of "cohesion", a line of code to get all the SQL statements.
No longer speaking, it is to introduce to you a practical application of the __call method. To fully understand it or recommend yourself to see the source of thinkphp! (Affirm: I have nothing to do with thinkphp, not to preach it well)
http://www.bkjia.com/PHPjc/631250.html www.bkjia.com true http://www.bkjia.com/PHPjc/631250.html techarticle This article to introduce the PHP __call method using instructions, there is a need to learn PHP __call method of Friends do not enter the reference. I believe that the first contact with PHP children's shoes, in the reference books have seen ...