从另一个类中的一个类创建对象

问题描述 投票:0回答:1

我在内核类的构造函数中从另一个类创建对象时遇到问题,它给出了致命错误,提示找不到该类,有人可以帮忙吗?我不知道怎么了?

namespace App\Http;

use App\Http\Config;
use App\Controllers;

class kernel{
    protected $controller = "HomeController";
    protected $action = "index";
    protected $params=[];
public function __construct(){
        $url = $this->ParseUrl();
        $format_url = ucfirst($url[0]) . "Controller";

        if (file_exists(Config::CONTROLLERS_PATH . $format_url . ".php")) {
            $this->controller = $format_url;
         }
//        $path = Config::CONTROLLERS_PATH . $this->controller . ".php";
//        echo "path: $path";// path is correct

    require_once (Config::CONTROLLERS_PATH . $this->controller . ".php");
//        $include_file = get_required_files();
//        var_dump($include_file);//require_once is working
//here is the problem
    $this->controller = new $this->controller;
}

 public function ParseUrl(){
        if (isset($_GET['url'])) {
        return explode("/", rtrim($_GET['url'], "/"));
        }
    }
}
php class object require-once createobject
1个回答
0
投票

尝试:

$this->controller = new {$this->controller};
© www.soinside.com 2019 - 2024. All rights reserved.