作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.
Cameron Barr
Verified Expert in Engineering

Cameron是一位才华横溢的PHP和JavaScript应用程序开发人员,具有工程背景和领导经验.

Read More

Expertise

PREVIOUSLY AT

Orion innovation
Share

Everyone has their own set of tools. As a PHP developer,我最喜欢的一个是快速应用开发框架“Nooku”。. In the words of the development groupNooku与其说是一个框架,不如说是一个web开发工具包。.

In case you are not familiar with it, have a look. 它是一个开放源码项目,大量使用业界公认的设计模式来生成高度组件化的应用程序,这些应用程序易于扩展和可重用(最初是由一个开发人员创建的) lead Joomla! developers). 开箱即用,Nooku为您提供了大量的快速应用程序开发工具来帮助您获得 projects off the ground faster. A small, but strong sample:

  • MVC的默认实现,你所需要做的就是编写布局(这就是吸引我的地方)
  • HMVC availability right away
  • 支持所有数据的不同输出格式,如JSON和XML.e., expose your API in minutes)
  • 默认的管理和前端实现

Nooku的核心是“组合优于继承”的设计原则(事实上, 这是Nooku介绍页面上的第一个概念. 一句话:您的目标应该是组合(或添加)多个对象的功能,以创建某种复合对象, rather than relying on subclassing.

PHP快速应用程序开发(RAD)中组合优于继承原则的可视化.

这一原则使您可以编写更少的代码,并经常产生一些非常优雅的解决方案. So how exactly is it promoted? Well, at the code level, 最好的例子来自Mixins和资源/服务标识符的使用. Lets take a look.

The Mixin

Before PHP 5.4, the language had no concept of Traits. These are class-like structures that, when ‘used’ by an object, 提供某种类型的功能(类似于多重继承). Nooku多年来一直在解决这个问题(从PHP 5开始).2) with the Mixin.

Mixin不仅让你把对象组合在一起, 但是它还将每个混合对象的方法添加到复合对象的接口中. 使用mixin的对象似乎“继承”了mixin对象的方法.

/**
    * Mixin an object
    *
    当使用mixin()时,调用对象继承mixin()对象的方法
    * in objects, in a LIFO order.
    *
    * @param KMixinInterface $object一个实现KMinxInterface的对象
    * @return  KObject
    */
public function mixin(KMixinInterface $object)
{
    $methods = $object->getMixableMethods($this);
    foreach($methods as $method) {
        $this->_mixed_methods[$method] = $object;
    }

    // Set the mixer
    $object->setMixer($this);

    return $this;

}

Nooku中的几乎所有对象都具有此功能,因为它们扩展了定义的基类KObject mixin method.

Nooku的控制器架构中的主要类也是从KObject派生的. 抽象控制器是KControllerAbstract类,通过检查,您可以看到它立即利用了混合能力. Whenever an instance of this class is constructed, KMixinCommand和KMixinBehavior功能被立即添加到它的接口. Consequently, Nooku中的每个控制器都通过各自的对象由命令链和行为管理功能组成.

Why the K in front of all the class names? Nooku’s main library is code named “Koowa”.

回到Nooku控制器:KMixinBehavior类保存了所有给KControllerAbstract特定加载能力的部分 Behaviors at runtime. 行为策略是描述流程或逻辑的类,这些流程或逻辑可以被其他类分离出来使用.g., editable, orderable). KMixinBehavior相当简单,只有四个方法:getBehavior, hasBehavior, addBehavior and getBehaviors. 这就是我们所需要给一个对象处理和封装不同行为策略的能力.

类似地,KMixinCommand只有三个方法:getCommandContext, getCommandChain, setCommandChain. If you hadn’t guessed, 这三个方法为KControllerAbstract提供了实现命令链的能力, but lets it do so at runtime.

你可以把这种混合想象成一个简单的算术加法:

描述Nooku的混合作为算术加法在这个快速应用程序开发框架.

Gives us an interface that looks like this:

这就是在这个特定的RAD框架中生成的接口.

根据定义,抽象类是用来扩展的,因此通过继承的魔力,所有KControllerAbstract的子对象或实例也获得了在运行时添加行为和命令链的能力.

Sounds cool. But what does that actually mean? In short, Nooku provides componentized functionality; that is, Nooku允许您将功能模块化,并在运行时跨模块组合功能.

这两个例子用来说明构图. 它们还用于展示Nooku RAD框架在其核心上对进一步组合的支持. This is an important advantage. 上面添加到KControllerAbstract中的方法支持“策略设计模式”,为开发人员提供了在编写一行代码之前封装变化的工具. mixin()方法是KObject的每个扩展的一部分,这意味着您可以在运行时轻松地为大多数对象定义和添加其他行为管理接口.

服务和资源标识符和定位符:将我的类名与我的对象解耦

Nooku中的服务和资源标识符和定位器也为关注点分离提供了强大的支持.

让我们再看看KObject,还有KService. 我们可以将Nooku中的大多数东西视为服务或资源, 因此,以完全相同的方式实例化和询问它们.

可以将服务视为从中获得资源的东西. 所有服务都是资源,但并非所有资源都是服务

当你去杂货店买东西的时候,把商店想象成服务.e., a collection of items that you can Browse; and the product as the Resource, i.e., a single item/solution logic that can be:


  • looked at specifically (Read) (e.g., looking at a can of tomato soup)
  • moved around the store (Edited) (e.g., move the soup to the produce aisle)
  • added to or removed from the store’s inventory (Add and Delete) (e.g.加入一种新的汤,去掉番茄)

Taking this example even further, 想象一下,杂货店有一个特许经营部门,你想做生意. In that situation, 服务是特许经营部门,资源是你买的杂货店. It is very much a contextual classification. As a whole, this is known as the BREAD (您将看到它们在KControllerService和KControllerResource之间以' _action '表示)。, i.e _actionRead()).

A model can be a service, a table object can be thought of as a service, 一个特定的MVC三元组被实例化为资源或服务, 而查询服务产生的特定记录可以被认为是资源.


Nooku中的每个对象都是对象的组合,因为每个对象都包含对“服务容器”中整个应用程序实例化服务的引用,以及访问这些服务的方法getService(). KObject::getService()方法所需要做的就是传递一个有效的资源标识符,然后它将返回一个实例化的服务以供使用.


In PHP rapid application development, 资源标识符为我们提供了一种强大的方式来将对象的实例化与其类名解耦, and thus provide aliases for that identification. 这对应用程序的可维护性有重要的影响. 通过别名,开发人员可以通过使用KService::addAlias()添加一行代码来更改每个对象使用的类,这些对象使用给定的标识符进行实例化。.

我们熟悉的资源标识符的一个例子是URI或统一资源标识符:

这个Nooku RAD框架教程中的一个示例资源标识符.

这是KService定位和加载适当类所需的所有信息. 这些部分与Nooku的类命名和放置约定相匹配,这些约定提供了放置和实例化的可预测性. 上面的标识符示例(com://site/user).database.table.User)尝试加载文件/components/com_user/databases/tables/ User.它的类名为ComUserDatabaseTableUser. Incidentally, 如果文件不存在,框架将给你一个默认的表对象,并根据数据库命名和id模式约定构建它(这更吸引我)。. 如前所述,KService还允许为标识符设置别名. Using KService:: setAlias(“maindbaseadapter”、“com: / / admin /违约.database.adapter.mysqli'); lets us load a db object with KService::getService(‘maindbaseadapter’).

这为我们提供了我们讨论过的解耦,并在应用程序的维护和扩展方面提供了显著的优势. 如果需要,我们可以自由创建除“site”和“admin”之外的应用程序,并且通过这里描述的标识符可以轻松地使用位于其他应用程序中的服务,以帮助我们的解决方案满足他们的要求. Again, 这是Nooku为PHP和RAD开发人员和团队提供的另一个例子,它不仅支持单个类对象的组合,还支持整个服务和应用程序的组合……而且是免费的.

Summing Up

With composition over inheritance at its heart; the smart, pre-existing compositions and structures that exist to support further amalgams; and the free service-oriented architecture with the identifiers described here, Nooku提供了一个强大的RAD框架,与任何同行相比都具有显著的领先优势 PHP development tools.

Hire a Toptal expert on this topic.
Hire Now
Cameron Barr

Cameron Barr

Verified Expert in Engineering

Adelaide, South Australia, Australia

Member since October 2, 2012

About the author

Cameron是一位才华横溢的PHP和JavaScript应用程序开发人员,具有工程背景和领导经验.

Read More
作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.

Expertise

PREVIOUSLY AT

Orion innovation

World-class articles, delivered weekly.

Subscription implies consent to our privacy policy

World-class articles, delivered weekly.

Subscription implies consent to our privacy policy

Toptal Developers

Join the Toptal® community.