繁体   English   中英

飞行php控制器类

[英]flight php controller class

我有这个控制器课

<?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();
}

}

您的辛格尔顿错了。 您永远不会实例化您的类,因此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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM