Home > Net >  Call to a member function generate() on null
Call to a member function generate() on null

Time:11-26

I am getting this error

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

when calling the action_index function;

This is the ControllerMain.php file; general_view.php is my layout;

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

File Core\Controller

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

File 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;
        }
    }

I could not find the right solutions for myself, so I decided to write here, so I hope for your help I ask you to help solve the problem, because I am new to php, and sorry for my bad english :) Perhaps I did not give all the information to understand what was going on, so if you need something, write to me

CodePudding user response:

Please correct me if I'm wrong, I guess you need parent::__construct(); in ControllerMain.

Example:

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

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

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