PHP Core idea of chain operation, PHP chain Core thought _php tutorial

Source: Internet
Author: User

PHP is the core idea of chain operation, and the core idea of PHP chain-type


The implementation of the PHP chain-type operation

Copy the Code code as follows:
$db->where ()->limit ()->order ();

Create a database.php under Common.

The core of chain operation is the following: return $this at the end of the method;

database.php:

<?phpnamespace common;class database{  function where ($where) {    return $this;  The core of the chain approach lies in the following: return $this  }  Function Order ($order) {return $this after each method    ;  }  function Limit ($limit) {    return $this;  }}

index.php:

<?phpdefine (' BASEDIR ', __dir__); Defines the root directory constants include BASEDIR. ' /common/loader.php '; Spl_autoload_register (' \\common\\loader::autoload '); $db = new \common\database (); Traditional operations require multiple lines of code implementation//$DB->where (' id = 1 ');//$db->where (' name = 2 ');//$db->order (' id desc ');//$db->limit (10) ;//using chained operation, a line of code solves the problem $db->where (' id = 1 ')->where (' name = 2 ')->order (' id desc ')->limit (10);

When using chained operations, the IDE (NetBeans will give you an automatic hint):

http://www.bkjia.com/PHPjc/1021093.html www.bkjia.com true http://www.bkjia.com/PHPjc/1021093.html techarticle PHP to implement the core concept of chain operation, PHP chain-like core idea of the implementation of the PHP chain operation code is as follows: $db-where ()-limit ()-order (); Create database.php under Common. Chain ...

  • 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.