0

I got a problem with my custom MVC code:

Fatal error: Uncaught Error: Cannot call constructor in C:\xampp\htdocs\phpmvc3\app\controller\admin.php:6 Stack trace: #0 C:\xampp\htdocs\phpmvc3\app\core\App.php(18): Admin->__construct() #1 C:\xampp\htdocs\phpmvc3\public\index.php(5): App->__construct() #2 {main} thrown in C:\xampp\htdocs\phpmvc3\app\controller\admin.php on line 6

Here's the code in App.php

<?php

class App {
    protected $controller = 'Home';
    protected $method = 'index';
    protected $params = [];

    public function __construct() {
        
        $url = $this->parseURL();

        if (!empty($url) && file_exists('../app/controller/' . $url[0] . '.php')) {
            $this->controller = $url[0];
            unset($url[0]);
        }

        require_once '../app/controller/' . $this->controller . '.php';
        $this->controller = new $this->controller;

        if (!empty($url) && isset($url[1])) {
            if (method_exists($this->controller, $url[1])) {
                $this->method = $url[1];
                unset($url[1]);
            }
        }

        // Params
        $this->params = !empty($url) ? array_values($url) : [];

        // Jalankan controller method dan kirimkan params jika ada
        call_user_func_array([$this->controller, $this->method], $this->params);
    }

    public function parseURL() {
        if (isset($_GET['url'])) {
            $url = rtrim($_GET['url'], '/');
            $url = filter_var($url, FILTER_SANITIZE_URL);
            $url = explode('/', $url);
            return $url;
        }
        return [];
    }
}

So I tried to load a library with using this code:

class Admin extends Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }

I wanted to load library but I'm not using CI3, I'm just using the MVC

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • `I'm not using CI3`...is this relevant somehow? If it's your custom code, that's fine, but it's unclear why you mention and tag this specific framework and version, if it's only to tell us that you aren't using it? – ADyson Jul 14 '23 at 16:00
  • @Blackwight: Please see the linked question. And decide: Use an MVC with its own class hierarchy or use a Framework (and that naturally has its own class hierarchy as well). From what I know, you can't create with the Codeigniter Framework an independent object hierarchy that is MVC only. But for your concrete question about the error: see the duplicate, don't call the parents' constructor (ctor) if there is none. Similar error as if you call an undefined method. – hakre Jul 14 '23 at 16:56

0 Answers0