PHP implements the core idea of chain operations. The core idea of PHP chain operations this article mainly introduces the core idea of PHP chain operations. This article focuses on its core idea, which is more intuitive and clear, for more information, see the core idea of PHP to implement chained operations.
This article mainly introduces the core idea of PHP implementing chain operations. This article focuses on its core idea, which is more intuitive and clear. For more information, see
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:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
Namespace Common;
Class Database {
Function where ($ where ){
Return $ this; // The core of the chained method is: return $ this after each method
}
Function order ($ order ){
Return $ this;
}
Function limit ($ limit ){
Return $ this;
}
}
|
Index. php:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
Define ('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 a chain operation and a line of code to solve the problem
$ Db-> where ('Id = 1')-> where ('name = 2')-> order ('Id desc')-> limit (10 );
|
When using the chain operation, ide (netbeans will automatically prompt ):
Ghost This article mainly introduces the core idea of PHP implementing chain operations. This article focuses on its core idea, which is more intuitive and clear. if you need it, you can refer to it...