"Go" php for consistent operation

Source: Internet
Author: User
Tags mysql command line

"The first scenario __call"

We often use this code when we are coding with frameworks such as thinkphp.

M (' User ')->where (array(' id ' =>1))->field (' name ')->select ();

This is not only conducive to coding, but also can make people "happy" it. Well, not much to say. Let's see how it's implemented, right?

<?PHP//database operation base class [PS: Main functional coherence function implementation]    classdb{//This property defines the name of the method to implement a coherent operation         Public $sql=Array(            "Field" and "" "," where "=" "," order "=" "," Limit "and" "," group "= =" "," having "and" "",        ); /** * Coherent operation, call the field () where () order () limit () group () The Have () method and combine it into an SQL statement * This method is a PHP magic method that calls a method that does not exist in the class and automatically With this method * @param $methodName call a non-existent method, the string that receives the method name * @param $args Call a method that does not exist, receive the parameter of this method as an array*/        function__call ($methodName,$args){            //To convert the method name to the request, unify to lowercase            $methodName=Strtolower($methodName); //if the request method name corresponds to a member attribute array $sql subscript, then the second parameter is assigned to the element corresponding to the subscript in the array.            if(isset($this->sql[$methodName])){                $this->sql[$methodName]=$args[0]; }Else{                Echo' Call class '.Get_class($this).‘ In the '.$methodName.‘ () method does not exist '; }            //returns the object so that you can continue to invoke the methods in this object to form a coherent operation            return $this; }        /** * Use this method to splice into a select SQL statement; [PS: This method ends a coherent operation and is placed on the last side of a coherent operation]*/        functionSelect () {//concatenation of SQL strings by the SELECT syntax [PS: You can perform a "help select" on the MySQL command line; View its grammatical structure]            $sql= "SELECT {$this->sql[' field '} from test {$this->sql[' where '} {$this->sql[' group '} {$this->sql[' having '} {$this->sql[' order '} {$this->sql[' limit ']} "; Echo $sql; }    }    $obj=Newdb (); $obj->field (' name,sex,address ')->where (' Where name= ' Gongwen ')->limit (' Limit 1 ')Select (); //output: SELECT name,sex,address from Test where Name=gongwen limit 1

Original source http://www.gwalker.cn/article-163.html

"The second option does not use __call"

This example is written below, it is not used _call, we can expand it.

/** SQL statement combination instance class, originating article Web development Note * For learning, non-professional class **/classsql{Private $sql=Array("from" + "", "where" = "", "order" = "" "," limit "=" "");  Public functionFrom$tableName) {        $this->sql["from"]= "from".$tableName; return $this; }       Public functionwhere$_where= ' 1=1 ') {        $this->sql["where"]= "where".$_where; return $this; }       Public functionOrder$_order= ' id DESC ') {        $this->sql["Order"]= "ORDER by".$_order; return $this; }       Public functionLimit$_limit= ' 30 ') {        $this->sql["Limit"]= "Limit 0,".$_limit; return $this; }     Public functionSelect$_select= ' * ') {        return"Select".$_select." ". (implode(" ",$this-sql)); }}  $sql=NewSQL (); 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

From the home of the script, the original author is unknown.

"Go" php for consistent operation

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.