1

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.

4
  • 1
    Try to include your Controller class in HomeController class Commented Jul 7, 2018 at 9:26
  • thanks! this is working! but is there another way so i dont have to include main controller in every single controller? Commented Jul 7, 2018 at 9:32
  • Include your, Controller on top of your index.php file Commented Jul 7, 2018 at 9:38
  • 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 Commented Jul 7, 2018 at 9:49

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks! now i see where my mistake is. first called homecontroller and then main controller so homecontroller didnt recognized my main controller.
Yeah you got it :)
Yes :) that's it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.