If you are a php programmer who has used multiple frameworks, you must have seen such a query statement.
If you are a php programmer who has used multiple frameworks, you must have seen such a query statement:
1 |
$ Result = $ mysqlDb-> limit ('0, 10')-> order ('Id desc')-> findall (); |
The preceding query statement uses the-> operator consecutively to perform operations and finally returns a query result. how can this be achieved.
Let's take a simple analysis:
-> The operator is used to access objects. the preceding statement uses a total of three-> operator, and the last return is the query result. This indicates that after the first two-> accesses, the returned result should be an object, because in php, if you use the-> operator for a non-object, it is impossible. This tells us that the limit and order methods in the $ mysql instance return a reference to the class itself, that is, return $ this, then we can achieve consistent query. See the sample code written by the webmaster:
12345678910111213141516171819202122232425262728293031323334353637 |
Limit = 'limit '. $ str; // set the limit statement // return the reference to the class itself. return new mysql_qery () cannot be used here. // This is equivalent to creating a new instance of the class, the limit statement set in the previous step does not exist in the new instance. // you can experiment with it yourself. // therefore, you need to return $ this, that is, the instance of the current class, return $ this ;} functionorder ($ str) {$ this-> order = 'Order '. $ str; // set the order statement return $ this; // return the reference to the class itself} functionfindall () {$ this-> SQL = 'select * from '. $ this-> tbl. ''. $ this-> order. ''. $ this-> limit; // concatenate an SQL statement echo $ this-> SQL; // The output is an example, so no code is written to query the database} } // Example $ mysqlDb = newmysql_query (); $ result = $ mysqlDb-> limit ('0, 10')-> order ('Id desc ') -> findall (); print_r ($ result);?> |
========================================================== ================================
In addition, some frameworks are implemented using the _ call () magic method.
123456789101112131415161718 |
Options [$ func] = $ args; return $ this; // this object is returned }}// Example $ test = newTest (); $ test-> form ('test'); // This call is equivalent to setting $ test-> options ['form'] = 'test '; |
// In ThinkPHP, such coherent operations end with find or findAll.
// Therefore, the preceding methods are called only by setting the query parameters.
// In the find or findAll method, different SQL statements are executed based on the $ this-> options parameter.
// For example
123456 |
Publicfunctionfind () {$ SQL = "SELECT {$ this-> options ['field']} FROM {$ this-> options ['form']}"; $ SQL. = isset ($ this-> options ['where'])? "WHERE {$ this-> options ['where']}": ''; // ...... more processing echo $ SQL ;} |
Here is a simple explanation, which may be a little different from the official version.
In ThinkPHP, the handsome methods are basically implemented in the _ call function, such as topN (), byXXX ();