DefiantPanda Posted April 6, 2018 Share Posted April 6, 2018 Wasn't sure which section to post this question in so putting it here... I am making my own app and trying to implement namespacing with composer but it isn't working. The directory structure of my application is as follows: /public_html ----/public --------index.php ----/src --------LoginController.php ----/vendor --------/phpunit --------/composer --------autoload.php ----composer.json In composer.json I have: "autoload": { "psr-4": { "Bills\\" : "src/" } } In my index.php I have: require_once __DIR__ . '/../vendor/autoload.php'; use Bills\LoginController; $actions = explode("/", $_SERVER['REQUEST_URI']); if($actions[1] != '') { $name = ucfirst($actions[1]) . "Controller"; $object = new $name($secret); } In LoginController.php I have 'namespace Bills;' at the top. But when I run it I get the error: Uncaught Error: Class 'LoginController' not found in /var/www/***/public_html/public/index.php Not sure what's going wrong... Link to comment Share on other sites More sharing options...
requinix Posted April 6, 2018 Share Posted April 6, 2018 Did you have Composer update after you modified the composer.json? Link to comment Share on other sites More sharing options...
DefiantPanda Posted April 6, 2018 Author Share Posted April 6, 2018 Yeah... I got it working by doing this - $name = ucfirst($actions[1]) . "Controller"; $controller = "Bills\\" . $name; $object = new $controller($secret); so it's new Bills\LoginController(); Not sure if that's how you're meant to do it, but it works so that's a relief Link to comment Share on other sites More sharing options...
kicken Posted April 6, 2018 Share Posted April 6, 2018 You can't use something that you refer to dynamically. Use directives are resolved at compile time before your script is processed and only affects static references. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.