在静态方法中访问配置
Access to config in static method
如何在控制器的静态方法中访问配置?我有 Phalcon 1.3 。此方法无效:
$offerSource = $this->config->offerSource;
如前所述,伪变量 $this 在静态方法中是不允许的。您可以将 config 属性 声明为静态的,然后在像这样的静态方法中访问它:self::$config
试试这个!
静态访问DI
如果需要,您可以访问在静态函数中创建的最新 DI 您可以在静态方法中使用 \Phalcon\DI::getDefault()
。 More Info
public function indexAction()
{
$this->StaticMethod();
}
private static function StaticMethod()
{
$config = \Phalcon\DI::getDefault()['config']->toArray();
echo "<pre>";
print_r($config);
echo "</pre>";
exit;
}
如何在控制器的静态方法中访问配置?我有 Phalcon 1.3 。此方法无效:
$offerSource = $this->config->offerSource;
如前所述,伪变量 $this 在静态方法中是不允许的。您可以将 config 属性 声明为静态的,然后在像这样的静态方法中访问它:self::$config
试试这个!
静态访问DI
如果需要,您可以访问在静态函数中创建的最新 DI 您可以在静态方法中使用 \Phalcon\DI::getDefault()
。 More Info
public function indexAction()
{
$this->StaticMethod();
}
private static function StaticMethod()
{
$config = \Phalcon\DI::getDefault()['config']->toArray();
echo "<pre>";
print_r($config);
echo "</pre>";
exit;
}