我的问题:
所以,我有一个名为’Home’的类,它扩展了类’Controller’,控制器类需要构造函数中的所有模型.这很好用.
现在我有一个名为’Login’的第二个类它也扩展了类’Controller’,但它没有调用构造函数,我很困惑为什么它不调用这个类中的构造函数.
我找到了一个解决方法,那就是使用Parent :: __ construct()在Login类中创建一个构造函数,当我这样做时,一切正常.
但我正在工作为什么它在Home类中工作,而不是在Login类中工作.
一些信息:
家庭控制器:(没有任何问题的那个)
当我不强制调用扩展类的构造函数时回溯:
0 Core\Controller->__construct() called at [/var/www/html/site.luukwuijster.eu/core/Router.php:26] #1 App->__construct() called at [/var/www/html/site.luukwuijster.eu/public/index.php:5]
当我强制调用扩展类的构造函数时回溯:
0 Core\Controller->__construct() called at [/var/www/html/site.luukwuijster.eu/app/controllers/home.php:12] #1 Home->__construct() called at [/var/www/html/site.luukwuijster.eu/core/Router.php:26] #2 App->__construct() called at [/var/www/html/site.luukwuijster.eu/public/index.php:5]
登录控制器:(有问题的那个)
当我不强制调用扩展类的构造函数时:
Fatal error: Uncaught Error: Class ‘App\User’ not found in /var/www/html/site.luukwuijster.eu/app/controllers/login.php:21 Stack trace: #0 /var/www/html/site.luukwuijster.eu/core/Router.php(26): Login->login() #1 /var/www/html/site.luukwuijster.eu/public/index.php(5): App->__construct() #2 {main} thrown in /var/www/html/site.luukwuijster.eu/app/controllers/login.php on line 21
当我强制调用扩展类的构造函数时回溯:
Core\Controller->__construct() called at [/var/www/html/site.luukwuijster.eu/app/controllers/login.php:11] #1 Login->__construct() called at [/var/www/html/site.luukwuijster.eu/core/Router.php:26] #2 App->__construct() called at [/var/www/html/site.luukwuijster.eu/public/index.php:5]
正如你所看到的,当我强制调用构造函数时,它工作正常,但是当我不强制它时,它只适用于Home类. (家庭控制器)
码:
家庭控制器:
use App\User;
use Core\Controller;
use Core\DB;
class Home extends Controller
{
public function test()
{
return User::name();
}
}
登录控制器:
use App\User;
use Core\Controller;
use Core\DB;
class Login extends Controller
{
//This is the constructor I was talking about to force the calling of
//the constructor in the extended class.
public function __construct()
{
parent::__construct();
}
public function login()
{
return User::name();
}
}
扩展类(控制器)
namespace Core;
class Controller
{
public $database;
function __construct()
{
//I have put this here to see the backtrace.
debug_print_backtrace();
$this->database = new DB();
foreach (glob('../app/models/*.php') as $model){
require_once $model;
}
}
Router.php
我把它放在这里是因为当我不强制在扩展类上调用构造函数时,它会在这里的规则26中调用.
($this-> controller = new $this-> controller;)
class App
{
protected $controller = 'home';
protected $method = 'index';
protected $parameters = [];
protected $a = 0;
public function __construct()
{
$url = $this->Url();
if(file_exists('../app/controllers/'. $url[0] .'.php')){
$this->controller = $url[0];
unset($url[0]);
} else {
if (isset($url[0]))
die(view('error404'));
}
require_once '../app/controllers/'. $this->controller .'.php';
$this->controller = new $this->controller;
if (isset($url[1])){
if (method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}else{
die(view('error404'));
}
}
$this->parameters = $url ? array_values($url) : [''];
echo call_user_func_array([$this->controller, $this->method], $this->parameters);
}
public function Url()
{
if(isset($_GET['url'])){
$url = filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL);
$exploded_url = explode('/', $url);
return $exploded_url;
}
}
}
我的问题:
为什么它在Home控制器中工作正常,而不是在Login Controller中?
我需要做些什么才能使其在登录控制器中以相同的方式工作?
我希望一切都清楚,如果你需要更多的代码或信息,你可以在评论中问我.