I´m using PHP5 to build my own MVC framework. I have a situation where all of my controllers are in the Controller namespace, as:
namespace Controllers
{
class Home extends \Framework\Core\Controller
{
...
}
}
At the application logic I need to choose wich controller to call from the URL passed from the call, as follows:
namespace Framework\Core;
class Application
{
protected $controllerName = 'home';
protected $methodName = 'index';
protected $controller;
public function __construct()
{
// Parse the received URL
$url = parseUrl();
// Choose the controller, if file exists
if (file_exists('../app/controllers/' . $url[0] . '.php'))
{
$this->controllerName = $url[0];
unset($url[0]);
}
require_once '../app/controllers/' . $this->controllerName . '.php';
// Call the controller
$objToCall = '\\Controllers\\' . $this->controllerName;
$this->controller = new $objToCall(); <<<======= PROBLEM HERE
... Proceed choosing the method and calling the objects method....
}
}
}
My problem is:
My controller file is called home.php, and my class is called Home (first letter uppercase). So the code above cannot find /Controllers/home class because the class is /Controllers/Home
On the other way, If I remove the namespace from the controller and call:
$this->controller = new $this->controllerName;
It works fine, even the name being home....
My questions:
a) Why the home is translated to Home if I have no namespaces involved ?
b) How can I solve that problem to call a class from a namespace without needing to know its uppercase/lowercase condition ?
Please do not suggest changing the first letter to uppercase as I may have classes like CustomerReceipt, PersonalCashWithdrawl, etc..
Thanks for helping.
\not/, so$objToCall = '\\Controllers\\' . $this->controllerName;