1

I am trying to understand and use the PSR-0 Autoloader. But it doesn't work.

My Folder structure:

Core/

  • Library/
  • Model/

My index.php in Root

<?php
require_once 'Core/Library/SplClassLoader.php';

$loader = new SplClassLoader('Core', 'Core');
$loader->register();



use Model\Post;

Post.php in Model folder.

<?php
namespace Model;

class Post implements PostInterface
{
// ...

PostInterface

<?php
namespace Model;

interface PostInterface
{
//...

I Get the following error:

Fatal error: Class 'Model\Post' not found in C:\wamp\www\Test\index.php on line 17

Line 17: Init new Post;

What am I doing wrong here?

John
  • 2,900
  • 8
  • 36
  • 65

1 Answers1

5

SplClassLoader works like this:

$loader = new SplClassLoader('NamespaceName', 'path/To/Base/Directory');

You are registering Core namespace here, but you don't have Core\Model namespace, but simply Model

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
  • You were correct, however, Now I finally got it to found the "Post model" but, I am having problem finding the PostInterface instead, Fatal error: Interface 'PostInterface' not found in C:\wamp\www\Test\Core\Model\Post.php – John Dec 08 '12 at 17:30
  • And `PostInterface.php` is located in your `Core/Model` directory? – dev-null-dweller Dec 08 '12 at 17:37
  • Yes, I simply missed out the Core\ part in PostInterface as well – John Dec 08 '12 at 17:38