如何避免手动将$ registry容器传递到我制作的每个新类的构造函数中? [英] How to avoid manually passing my $registry container into constructor of every new class I make?

查看:86
本文介绍了如何避免手动将$ registry容器传递到我制作的每个新类的构造函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做 MVC 已有几个月了,并将所有内容存储在我的 $ registry 对象中.创建新类时,通常只会通过注册表,但是创建新类时必须不断通过$this->registry.

I've been doing MVC for several months now, and I store everything in my $registry object. When I create a new class, I only ever pass the registry usually, but I'm having to constantly pass the $this->registry when creating a new class.

例如

class something
{
   public function __construct($registry)
   {
      $this->registry = registry;
      $this->db = $registry->db;
      $this->user = $registry->user; // ......
   }

   public function something()
   {
      $class = new something_class($this->registry);
      $class->do();
   }
}

class something_class
{
   public function __construct($registry)
   {
      $this->registry = $registry;
   }

   public function do()
   {
     echo 'Doing something ....';
   }
}

我的问题是,如何处理注册表类内部的幕后传递注册表到新类(在这种情况下,当实例化something_class时)?我绝对相信有一个简单的方法可以做到这一点,但是我找不到任何与我要寻找的东西相关的东西.

My question is, how can I handle the passing of the registry to the new class behind the scenes (in this case when instantiating something_class) inside the registry class somehow? I'm absolutely convinced there is an easy way to do this, but I can't find anything related anywhere to what I'm looking for.

这是我的注册表课程:

<?php
class registry
{
    protected $vars = array();
    public function &__set($index, $value)
    {
        $this->vars[$index] = $value;
        return $value;
    }

    public function &__get($index)
    {
        return $this->vars[$index];
    }
}

推荐答案

这都是错误的. 注册表"是一种反模式,您正在做的是不依赖项注入.您已经找到一种伪造全局变量的方法.就是这样.

This is all wrong. "Registry" is an anti-patter and what you are doing the is not dependency injection. You have found a way to fake global variables .. that's it.

首先,请观看本讲座.

至于如何正确地做自己想做的事情,有两种方法:

As for, how to correctly do what you want, there are two ways:

  • 使用工厂,该工厂使用您提供的依赖项创建一个类
  • 使用依赖项注入容器, Auryn

要了解如何使用DI容器,您只需查阅文档.但是我将解释工厂的基础知识,这更像是一种DIY方法

To learn how you use a DI container, you will just have to consult the documentation. But I will explain the basics of factory, which is more of a DIY approach

工厂是一个对象,负责初始化其他类.例如,您有大量的类,这些类需要PDO作为依赖项.

A factory is an object, that is responsible for initializing other class. For example, you have a large set of classes, which require PDO as a dependency.

class Factory 
{
    private $pdo;

    public function __construct(PDO $pdo) {
        $this->pdo = $pdo;
    }


    public function create($name) {
        return new $name($this->pdo);
    }
}

如果使用此类的实例,它将允许您创建对象,这些对象已经将PDO作为依赖项传递给构造函数:

If you use an instance of this class, it would let you create objects, which have the PDO already passed in as a dependency in a constructor:

$factory = new Factory(PDO($dsn, $user, $pass));
$user = $factory->create('User');
$document = $factory->create('Doc');

另一个好处是,此设置将使User类实例和Doc类实例成为bot来共享同一PDO对象.

And as an added benefit, this setup would let bot the User class instance and the Doc class instance to share the same PDO object.

这篇关于如何避免手动将$ registry容器传递到我制作的每个新类的构造函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆