PHP coherent operations implementation method _ PHP Tutorial

Source: Internet
Author: User
PHP class coherent operation implementation method. PHP coherent operation implementation method PHP consistent operation looks really cool, it is also very convenient to read the code, of course, it must be used in OOP, in the process of the program, implementation of PHP-like coherent operations

Coherent operations in PHP look really cool and very convenient for reading code. of course, they must be used in OOP. in procedural programs, this method is not necessary. We can use _ CALL to implement this method. The example I wrote below does not use _ call. you can extend it.

The SQL statement combination class written below is mainly used for learning. if you want to use it, please complete it.

/*
* SQL statement combination instance class, originating article web development notes
* Www.chhua.com
* Used for learning, non-professional
**/
Class SQL {
Private $ SQL = array (from =>,
Where =>,
Order =>,
Limit => );

Public function from ($ tableName ){
$ This-> SQL [from] = FROM. $ tableName;
Return $ this;
}

Public function where ($ _ where = '1 = 1 '){
$ This-> SQL [where] = WHERE. $ _ where;
Return $ this;
}

Public function order ($ _ order = 'Id desc '){
$ This-> SQL [order] = ORDER BY. $ _ order;
Return $ this;
}

Public function limit ($ _ limit = '30 '){
$ This-> SQL [limit] = LIMIT 0,. $ _ limit;
Return $ this;
}
Public function select ($ _ select = '*'){
Return SELECT. $ _ select... (implode (, $ this-> SQL ));
}
}

$ SQL = new SQL ();

Echo $ SQL-> from (testTable)-> where (id = 1)-> order (id DESC)-> limit (10)-> select ();
// Output SELECT * FROM testTable WHERE id = 1 order by id desc limit 0, 10

The coherent operations in PHP look really cool and very convenient for reading code. of course, they must be used in OOP. in procedural programs, just...

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.