在 null 上调用成员函数 generate()

Call to a member function generate() on null

我遇到了这个错误

"Fatal error: Uncaught Error: Call to a member function generate() on null in /var/www/example.loc/app/Controllers/ControllerMain.php on line 17"

调用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');
    }

文件Core\Controller

    <?php
    
    namespace Core;
    
    class Controller {
        public $model;
        public $view;
    
        public function __construct()
        {
            $this->view = new View();
        }
    
        public function action_index()
        {
        }
    }

文件Core/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 的新手,抱歉我的英语不好 :) 也许我没有提供所有信息以了解发生了什么,所以如果您需要什么,请写信给我

如有错误请指正,我猜你需要parent::__construct(); in ControllerMain

示例:

class ControllerMain extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->model = new Users();
    }

    public function action_index()
    {
        $this->view->generate('general_view.php');
    }