PHP implementation of coherent operation, chain operation Instance _php instance

Source: Internet
Author: User

The consistent operation in PHP looks really cool, and very convenient to read the code, of course, must be used in OOP, in the process of the program, there is no need to use this method. There is a useful _call to implement this method, and I write the following example, not with the _call, we can expand it.

The following is written in the SQL statement combination class, mainly used for learning, if there are students want to use, please further refine.


 * * * SQL statement combination instance class, originating article Web development notes
 * Learning, non-professional
 * * *
/class sql{
	private $sql =array ("from" => "",
			" where "=>" ",
			" "Order" => ""
			, "Limit" => "");
 
	The public function from ($tableName) {
		$this->sql[' from ']= ' from '. $tableName;
		return $this;
	}
 
	The 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 the ORDER by ID DESC LIMIT 0,10

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.