tags:

views:

7

answers:

1

Hello,

I'm writing my own little MVC and I got a question... I'm passing $_SERVER['REQUEST_URI'] to the Router class which returns me controller, it's action and array of GET parameters. (For example Router said that I should call App_Controller->index()).

So, I should create instance of App_Controller and call index() method. So, I'm doing something like $controller = new $controller_name etc. But what if controller is not found by spl_autoload_register() (I'm using autoload).

How can I generate Exception about the fact that controller is not found? Or maybe I'm doing something wrong?

Thank you.

A: 

You could check it using class_exists which has a $autoload parameter (set to true as default, and therefore tries to autoload the class).

if(!class_exists($controller_name)) throw new Exception('Controller does not exist'); 
$controller = new $controller_name();
Max
Great, thank you so much! Absolutely the same about checking if class method exists, right? (using `method_exists`)
Kirzilla
@Kirzilla actually, yes, works the same way and considers autoloading. I used it myself in a similiar situation (and was actually surprised that it worked with autoloading ;)).
Max