PHP implementation of the coherent operation, chain Operation example, PHP chain Example _php Tutorial

Source: Internet
Author: User

PHP implementation of the coherent operation, chain Operation example, PHP chain example


The consistent operation in PHP looks really cool, it's very handy for code reading, of course, it has to be used in OOP, and in a procedural process, there's no need to use this method. There are useful _call to implement this method, and I write the following example, it is not used _call, we can expand it.

The following write this SQL statement combination class, is mainly used for learning, if there are students want to take to use, please perfect again.

/* * SQL statement combination instance class, originating article Web development Note * 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 by". $_order;return $this;} Public function limit ($_limit= ') {$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 ()->select ();//Output SELECT * From TestTable WHERE id=1 ORDER by ID DESC LIMIT 0,10


thinkphp Development Guide-how the coherent operation of the model can be achieved

Very simple, first you have 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 ();

Is the same, mainly when the 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

Static Class of PHP static class how coherent operations such as dynamic classes of coherent operation $x->a ()->b ()->c (); (I hope ghosts are only pointing)

You're sick. Static classes are generally a way of wrapping a function method do one thing

A dynamic class is a coherent operation that simply changes the return value of a method to $this, and operates on the properties of a class.

Static classes are generally not manipulating the properties of a class.

http://www.bkjia.com/PHPjc/840758.html www.bkjia.com true http://www.bkjia.com/PHPjc/840758.html techarticle PHP Implementation of the coherent operation, chain operation instances, PHP chain-like example PHP coherent operation looks really cool, but also very convenient code reading, of course, must be used in OOP, ...

  • Related Article

    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.