using the SplClassLoader I keep getting class not found
error. I tried various sources, copied their exact folder structure and naming but still not found
.
My structure
application
- Router
- Exceptions
- HttpException.php
- Klein.php
index.php
SplClassLoader.php
index.php
$loader = new SplClassLoader('application', 'application');
$loader->register();
$klein = new \router\Klein();
Klein.php
namespace application\router;
class Klein{
__construct()
HttpException.php
namespace Klein\Exceptions;
use RuntimeException;
class HttpException extends RuntimeException implements HttpExceptionInterface
{
How do I include both these with the classLoader?
The exact error is:
Fatal error: Class 'router\Klein' not found in /home/i366963/domains/[domain]/private_html/public/index.php on line 44
line 44 is $klein = new \router\Klein();
Suggestion:
$loader = new SplClassLoader('application', 'application');
$loader->register();
$klein = new application\Router\Klein();
giving error:
Warning: require(application/application/Router/Klein.php): failed to open stream: No such file or directory in /home/i366963/domains/[domain]/private_html/SplClassLoader.php on line 140
Fatal error: require(): Failed opening required 'application/application/Router/Klein.php' (include_path='.:/usr/local/lib/php') in /home/i366963/domains/[domain]/private_html/SplClassLoader.php on line 140
So looking at the signature of SplClassLoader
, it expects two parameters
$ns (namespace)
$includePath
So right now you're adding the namespace of 'application' and searching in the 'application' directory.
That would mean that mean that the application name space will be prefixed by everything found in the application directory.
So when you're loading \application\router it's looking in the application\application directory.
You can solve by that by doing $loader = new SplClassLoader('application');
which means it'll just search from the current directory forward.
Now looking at your other posted classes you're going to run into issues are your namespaces aren't following the PSR-0 standard, so you will need to make sure your namespaces match your directory structure and File name exactly (it's case sensitive on any *nix based system)