我收到此錯誤
“致命錯誤:未捕獲的錯誤:在第 17 行的 /var/www/example.loc/app/Controllers/ControllerMain.php 中呼叫成員函式 generate() 為 null”
呼叫 action_index 函式時;
這是 ControllerMain.php 檔案;general_view.php 是我的布局;
<?php
namespace App\Controllers;
use App\Models\Users;
use Core\Controller;
class ControllerMain extends Controller
{
public function __construct()
{
$this->model = new Users();
}
public function action_index()
{
$this->view->generate('general_view.php');
}
檔案核心\控制器
<?php
namespace Core;
class Controller {
public $model;
public $view;
public function __construct()
{
$this->view = new View();
}
public function action_index()
{
}
}
檔案核心/view.php
<?php
namespace Core;
class View
{
//public $template_view; // здесь можно указать общий вид по умолчанию.
function generate($general_view, $data = null)
{
if(is_array($data)) {
// преобразуем элементы массива в переменные
extract($data);
}
include 'app/views/'.$general_view;
}
}
我找不到適合自己的解決方案,所以我決定寫在這里,所以我希望得到你的幫助 我請你幫助解決這個問題,因為我是 php 新手,很抱歉我的英語不好:) 也許我做到了不要提供所有資訊以了解發生了什么,所以如果您需要什么,請寫信給我
uj5u.com熱心網友回復:
如果我錯了,請糾正我,我想你需要parent::__construct();在ControllerMain。
例子:
class ControllerMain extends Controller
{
public function __construct()
{
parent::__construct();
$this->model = new Users();
}
public function action_index()
{
$this->view->generate('general_view.php');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/365919.html
下一篇:JSON陣列到逗號分隔的字串
