我的controller.php看起来像是:https://github.com/panique/mini/blob/master/application/core/controller.php
我的相册.php看起来像是:https://github.com/panique/mini/blob/master/application/controller/songs.php
专辑扩展控制器。我想“创建”客户端i controller.php,并能够在album.php中的所有功能中使用它
Controller.php这样
class Controller
{
function __construct()
{
$this->openDatabaseConnection();
$this->loadModel();
$this->aws();
}
public function aws()
{
$client = S3Client::factory(array(
'credentials' => array(
'key' => AWS_KEY,
'secret' => AWS_SECRET,
)
));
}
}
album.php
class Album extends Controller
{
public $client;
function __construct($client)
{
parent::__construct();
$this->client = $client;
}
public function index()
{
//access $this-client here
}
public function myfunction()
{
//access $this-client here
}
}
换句话说,我想在类album.php
中的所有函数中使用$ client错误
未定义的变量:album.php中的客户端
答案 0 :(得分:0)
像这样:
<?php
class Controller
{
public $client;
public $dbConn;
function __construct(DatabaseClass $dbConn)
{
// $this->openDatabaseConnection(); // Database connections should be passed in as a dependency via the construct, just a suggestion
$this->dbConn = $dbConn;
$this->loadModel();
$this->aws();
}
public function aws()
{
$this->client = S3Client::factory(array(
'credentials' => array(
'key' => AWS_KEY,
'secret' => AWS_SECRET,
)
));
}
}
不需要在这里有$ client
class Album extends Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
//access $this-client here
}
public function myfunction()
{
//access $this->client here
}
}