PHP implementation of coherent operations, chained operation instances, php chained instance _ PHP Tutorial

Source: Internet
Author: User
PHP-implemented coherent operations, chained operation instances, and php chained instances. PHP-implemented coherent operations and chained operation instances. php-chained operations in PHP seem cool and very convenient for reading code. of course, they must be used in OOP, PHP-implemented coherent operations, chained operation instances, php chained instances

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 * for learning, non-professional class **/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 ". $ _ 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


ThinkPHP Development Guide-How to Implement model-related operations

First, you need to understand the difference between D () and M.

Here is a simple example:
$ Model = M ('User _ list ');

$ Model-> where ('User _ name = "Hello" ')-> select ();

And

$ Model-> where ('User _ name = "Hello "');

$ Model-> limit (5)-> select ();

The same is true. When an end function such as select () find () findAll () appears, the language is executed. Otherwise, it can be assembled all the time.

Location in

Lib/Think/Core/Model. class. php

Php static problems How does a static class perform coherent operations such as dynamic class operations $ x-> a ()-> B ()-> c (); (hope someone else can advise)

You are abnormal. static classes generally use function methods to encapsulate a method to do one thing.

For a dynamic class, you only need to change the return value of the method to $ this. this operation operates on the attributes of a class.

Static classes are generally not operational class attributes.
 

Coherent operations in PHP look really cool and very convenient for reading code. of course, they must be used in OOP ,...

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.