【问题标题】:PHP dependency injection - difference between a container, a controller or a factoryPHP 依赖注入 - 容器、控制器或工厂之间的区别
【发布时间】:2014-11-13 17:24:06
【问题描述】:

我仍然无法区分容器、控制器或工厂。比如下面的代码,它应该被认为是一个容器、一个控制器还是一个工厂?

如果是这种情况下的容器应该怎么样?

namespace ioc
{
    class LoggerContainer
    {
        use \Snippets;

        /*
         * Set props.
         */
        protected $Database;
        public $Logger;

        /**
         * Construct data.
         */ 
        public function __construct($Database) 
        {
            // Set DI.
            $this->Database = $Database;

            // Run private method.
            $this->getLogger();
        }

        /**
         * Set a new class (instantiate the class) as the return result.
         */
        private function getLogger()
        {
            $signature = AUTHENTICATED_USER_SIGNATURE;
            $this->Logger = new \core\model\Logger($this->Database);

            if($_REQUEST['url'] === 'backoffice')
            {
                return $this->Logger->setLogger($signature)->addCategory()->addSomething();
            }
            else
            {
                return $this->Logger->setLogger($signature)->removeSomething();
            }

        }
    }
}

【问题讨论】:

    标签: php dependency-injection containers factory


    【解决方案1】:
    • 这是一个容器,因为它是一个包含另一个正在访问的对象的对象,在这种情况下是一个 Logger。

    • 控制器是一个术语,通常指MVC的C,它控制着模型和视图

    • 虽然工厂方法是指返回接口/抽象类的正确特化的例程

    请注意,这三个术语指的是非常不同的事物

    【讨论】:

    • 感谢您的编辑。我可以理解容器和控制器的前两个。但不确定returns the correct specialization of an interface/abstract class - 你能给我看一个工厂的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2010-09-13
    • 2014-09-15
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2021-05-08
    相关资源
    最近更新 更多