Php Framework-how php framework implements consistent query

Source: Internet
Author: User
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 ();

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.