类似PHP Jquery的嵌套可读性

类似PHP Jquery的嵌套可读性,php,frameworks,readability,Php,Frameworks,Readability,我正在构建一个新的框架,在所有的工作之下,一个奇怪的问题唤起了我的思想。是否值得创建类似jquery的嵌套语法 core->component->…->method() 假设所有“组件”都扩展了一个抽象类,其核心如下 class core { public $components = array(); public load ($component) { //Assuming component exists include($comp

我正在构建一个新的框架,在所有的工作之下,一个奇怪的问题唤起了我的思想。是否值得创建类似jquery的嵌套语法

core->component->…->method()

假设所有“组件”都扩展了一个抽象类,其核心如下

class core {
     public $components = array();
     public load ($component) {
          //Assuming component exists
          include($component);
          $this->component = new component_name($parameters);
          //This last line might be as well included in "component"'s constructor
          $this->component->core_link = $this;

          $components[] = $component;
     }
}
这个“架构”是对的还是在PHP中听起来很奇怪? 它容易阅读/学习吗? 你能给我一些小费吗


感谢

有许多PHP框架/库使用这种流畅的编程风格(也称为方法链接、方法级联等)。因此,我认为这种方法没有任何问题。我个人经常使用它

然而,您的实现是不正确的。基本上,每个受链接约束的方法调用都必须返回一个实例化对象,以允许对其进行后续方法调用

例如,假设我想修改您的
load()
方法,以允许在加载的新
组件名称上链接。可能是这样的:

 public load ($component) {
      //Assuming component exists
      include($component);
      $this->component = new component_name($parameters); // not sure where $parameters comes from in your code here
      $components[] = $component;
      return $this->component;
 }
其用途是:

$core_object->load('some_component_name')->some_method_on_component_name();

感谢您的回复,因为“load”本身就是一个我称之为“死端”的组件,所以不允许进一步链接,必须调用一个方法来结束链接。感谢您分享我渴望但找不到的名称,并指出如何进一步链接。@Kei有时人们会选择对所有不希望返回离散值的方法(如getter方法)执行
return$this
,即使是那些您认为是“死胡同”的方法。这是为了使链接变得可靠。示例用法:
$components\u包括新加载的\u one=$core\u object->load('xyz')->get\u components()