通常,在许多框架中,您可以找到使用查询构建器创建查询的示例.你经常会看到:
Commonly, in a lot of frameworks, you can find examples of creating a query using the query builder. Often you will see:
$query->select('field');
$query->from('entity');
但是,在某些框架中你也可以这样做
However, in some frameworks you can also do it like this
$object->select('field')
->from('table')
->where( new Object_Evaluate('x') )
->limit(1)
->order('x', 'ASC');
您实际上是如何制作这种链条的?
How do you actually do this kinds of chains?
这叫做 Fluent Interface -- 该页面上有一个 PHP 示例.
This is called Fluent Interface -- there is an example in PHP on that page.
基本思想是该类的每个方法(您希望能够链接) 必须返回 $this -- 这使得调用其他方法成为可能返回的 $this 上同一个类的方法.
The basic idea is that each method (that you want to be able to chain) of the class has to return $this -- which makes possible to call other methods of that same class on the returned $this.
当然,每个方法都可以访问类的当前实例的属性——这意味着每个方法都可以向当前实例添加一些信息".
And, of course, each method has access to the properties of the current instance of the class -- which means each method can "add some information" to the current instance.
这篇关于方法链 PHP OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
以编程方式将可下载文件添加到 Woocommerce 产品Add programmatically a downloadable file to Woocommerce products(以编程方式将可下载文件添加到 Woocommerce 产品)
获取今天 Woocommerce 中每种产品的总订单数Get today#39;s total orders count for each product in Woocommerce(获取今天 Woocommerce 中每种产品的总订单数)
在 WooCommerce 和电话字段验证问题中添加自定义注Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和电话字段验证问题中添加自定义注册字段
在 Woocommerce 简单产品中添加一个将更改价格的选Add a select field that will change price in Woocommerce simple products(在 Woocommerce 简单产品中添加一个将更改价格的选择字段)
在 WooCommerce 3 中将自定义列添加到管理产品列表Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中将自定义列添加到管理产品列表)
自定义结帐“下订单"按钮输出htmlCustomizing checkout quot;Place Orderquot; button output html(自定义结帐“下订单按钮输出html)