0

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
Community
  • 1
  • 1
Rien
  • 398
  • 2
  • 12
  • 37
  • Post the exact error message in it's entirety please. – Jonathan Feb 18 '17 at 16:08
  • @Augwa added the error on the bottom. – Rien Feb 18 '17 at 16:13
  • your namespace is \application\router, not \router. change to new \application\router\. As well if you're running this on Unix your directory name is case sensitive, which means you either need to change name of the directory to `router` or update your namespace to be `application\Router` – Jonathan Feb 18 '17 at 16:27
  • @Augwa that returns me an `failed to open stream` error (added at bottom of post). I keep bouncing between class not found and file not found. – Rien Feb 18 '17 at 17:50

1 Answers1

2

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)

Community
  • 1
  • 1
Jonathan
  • 2,778
  • 13
  • 23