日安。
我有一个PDO (PostgreSQL)对象在我的Phalcon框架中执行数据库中的操作,问题是我正在实现我找到的一些Oauth服务器类,并且需要将这个对象传递给Oauth服务器的类,这样它就可以执行操作到数据库.
以下是创建类的方式:
数据库对象:
$di['db']
以及我是如何把它送到课堂上的:
// Initiate the auth server with the models
$server = new League\OAuth2\Server\Resource(
new League\OAuth2\Server\Storage\PDO\Session($di['db'])
);
Session.php文件:
<?php
namespace League\OAuth2\Server\Storage\PDO;
use League\OAuth2\Server\Storage\SessionInterface;
class Session implements SessionInterface
{
public functions that will need to use the database object...
但是对象在Session.php文件中不可用,所以我如何从新的类中访问它?
谢谢。
发布于 2014-04-14 18:11:08
声明私有变量并在Session.php中向类添加构造函数
private $database;
public function __construct($database) {
$this->database = $database;
}
实例化会话对象之后,您将能够访问会话对象的方法中的数据库对象。
$this->database->do_something();
我建议在构造函数中添加暗示类型,但是由于我不知道$database应该是什么对象类型,所以我将退出它。您可以在这里阅读更多关于类型提示的内容:
https://stackoverflow.com/questions/23073380
复制相似问题