我有这个控制器类
<?php
namespace App\Controllers;
use Application\Controller;
use Application\FlightApplication;
class TestController extends Controller {
public function test(){
$this->app->logger->debug('test');
echo 'test';
}
}
扩展了这个类
<?php
namespace Application;
class Controller {
public $app = null;
public function __construct(){
$this->app = FlightApplication::getInstance();
}
}
当我运行这条路线时:
Flight::route('/test', array('App\Controllers\TestController','test'));
我收到此错误:
致命错误:不在对象上下文中时使用$ this
为什么?
这是FlightApplication
类:
<?php
namespace Application;
class FlightApplication {
private static $instance = null;
public function __construct(){
self::$instance = $this;
}
/**
*
* @return \Application\FlightApplication
*/
public static function getInstance(){
return self::$instance;
}
public function start(){
\Flight::start();
}
}
答案 0 :(得分:0)
你的单身人士错了。您永远不会实例化您的类,因此get函数始终返回NULL
。您需要检查以实例化类
public static function getInstance(){
if(!self::$instance) self::$instance = new self();
return self::$instance;
}
此外,您应该从构造函数中取出设置
public function __construct(){
self::$instance = $this; // Remove this line as your Singleton handles this
}