Php ZF2如何将适配器放入注册表

Php ZF2如何将适配器放入注册表,php,zend-framework,zend-framework2,Php,Zend Framework,Zend Framework2,我在应用程序中使用zend类(没有mvc)。我想将适配器实例放入容器中,并在其他类中使用它。这就是我现在拥有的 load.php $adapter = new Zend\Db\Adapter\Adapter(array( 'driver' => 'pdo_mysql', 'database' => DB_PREFIX.DB_NAME, 'username' => DB_USER,

我在应用程序中使用zend类(没有mvc)。我想将适配器实例放入容器中,并在其他类中使用它。这就是我现在拥有的

load.php

$adapter = new Zend\Db\Adapter\Adapter(array(
            'driver'   => 'pdo_mysql',
            'database' => DB_PREFIX.DB_NAME,
            'username' => DB_USER,
            'password' => DB_PW
    ));
 public class OtherClass{

     function __construct() {       
        //$adapter is needed here
        $this->_model = get_class($this);
        $this->sql = new Sql\Sql($this->dbo);
    }
}
如何在其他类中访问$adapter

OtherClass.php

$adapter = new Zend\Db\Adapter\Adapter(array(
            'driver'   => 'pdo_mysql',
            'database' => DB_PREFIX.DB_NAME,
            'username' => DB_USER,
            'password' => DB_PW
    ));
 public class OtherClass{

     function __construct() {       
        //$adapter is needed here
        $this->_model = get_class($this);
        $this->sql = new Sql\Sql($this->dbo);
    }
}
以前在ZF1中有一个Zend::Registry,我可以在其中设置变量。然后,稍后通过调用get方法再次访问它。但是,我在ZF2中找不到类似的案例。我尝试使用ServiceManager,但在我的场景中它似乎不起作用


非常感谢专家的建议。

ZF2没有像ZF1那样的
注册表。相反,我们有所谓的
ServiceManager
。您应该使用此SM将依赖项注入到服务中

您可以在此处阅读有关SM的更多信息->


我建议在github->上签出ZfcUser并签出他们的Module.php文件。它可能会帮助您更好地理解工作示例如何使用SM处理注入。

感谢您的回复。ServiceManager是否适用于自定义框架?我没有使用Zend Framework 2,您也可以将其作为独立版本使用,是的。要了解这是如何做到的,我想目前最好的方法是查看ServiceManager的单元测试;)