PHP implements the core idea of chain operations, and php chain core idea

Source: Internet
Author: User

PHP implements the core idea of chain operations, and php chain core idea

Implementation of PHP chained operations

 Copy codeThe Code is as follows:
$ Db-> where ()-> limit ()-> order ();
 

Create Database. php under Common.

The core of a chain operation is: return $ this at the end of the method;

Database. php:

<? Phpnamespace Common; class Database {function where ($ where) {return $ this; // the core of the chained method is: after each method, return $ this} function order ($ order) {return $ this;} function limit ($ limit) {return $ this ;}}

Index. php:

<? Phpdefine ('basedir' ,__ DIR _); // defines the root directory constant include BASEDIR. '/Common/Loader. php '; spl_autoload_register (' \ Common \ Loader: autoload'); $ db = new \ Common \ Database (); // traditional operations require multi-line code implementation // $ db-> where ('Id = 1'); // $ db-> where ('name = 2 '); // $ db-> order ('Id desc'); // $ db-> limit (10); // use chained operations, one line of code solves the problem $ db-> where ('Id = 1')-> where ('name = 2')-> order ('Id desc ') -> limit (10 );

When using the chain operation, ide (netbeans will automatically prompt ):

 

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.