1
votes

I have main index.php file where i call other controllers and that is fine.

I call them with call_user_func_array( [ $obj, $method[ 0 ] ], $params );

For example i first include controller and then i call it. My controller is called HomeController and this is example how i include it and call it.

require( 'system/mvc/controller/'. $controller .'.php' );
$obj = new $controller;
call_user_func_array( [ $obj, $method[ 0 ] ], $params );

And that is ok and its working.

So my system/mvc/controller/HomeController.php looks like this:

<?php

class HomeController
{

    public function test()
    {
        echo 'Something!';
    }

   }

    ?>

And this is ok. Now i was tried to include main controller.php file that will extends my HomeController. So in my index.php file i included system/lib/php/Controller.php and code now looks like this in index.php

require( 'system/mvc/controller/'. $controller .'.php' );
require( 'system/lib/php/controller.php' );
$obj = new $controller;
call_user_func_array( [ $obj, $method[ 0 ] ], $params );

And in HomeController i used class HomeController extends Controller and i got this error: Fatal error: Class 'Controller' not found in /var/www/html/system/mvc/controller/HomeController.php on line 3

Tried to use namespaces but that didnt work.

1
Try to include your Controller class in HomeController class - Sourabh
thanks! this is working! but is there another way so i dont have to include main controller in every single controller? - fr33jumper
Include your, Controller on top of your index.php file - Sourabh
i suggest you learn about composer, and its autoloading functionality, so you don't need to require files manually. composer has become industry standard over the last 6 years: getcomposer.org/doc/01-basic-usage.md#autoloading - Bouke Versteegh

1 Answers

1
votes

Try to change

require( 'system/mvc/controller/'. $controller .'.php' ); require( 'system/lib/php/controller.php' );

to

require( 'system/lib/php/controller.php' ); require( 'system/mvc/controller/'. $controller .'.php' );