PHP implements the core idea of chain operations, and php chain core idea. PHP implements the core idea of chain operations. php chain core idea the code for copying PHP chain operations is as follows: $ db-where ()-limit ()-order (); create a Database under Common. php. Chain PHP implements the core idea of chain operations, and php chain core idea
Implementation of PHP chained operations
The 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 ):
The implementation code of the batch PHP chained operation is as follows: $ db-where ()-limit ()-order (); create Database. php under Common. Chain...