André Figueira
André Figueira

Reputation: 6696

PHP OOP autoloading class namespaces issue

I am trying to upgrade the code of a new project I am working on to comply with PSR-0.

I am using and SPL loader class, However I may be doing something wrong, I just can't spot what the issue is.

I keep getting the following error:

Fatal error: Class 'widezike\General' not found in /nfs/c03/h04/mnt/169128/domains/widezike.com/html/beta/lib/functions.php on line 14

This is my folder structure:

index.php
-lib
config.php
init.php
spl-class-loader.php
functions.php
    -widezike
        -General.php

This is my functions file where it begins anything to do with the server side code:

<?php

include 'init.php';
include 'config.php';

include 'spl-class-loader.php';

$loader = new SplClassLoader('General', 'lib/widezike');
$loader->register();

use widezike\General;

//Run the output buffer
General::ob();

So this is my code for now, but I can't seem to find what's causing the fatal error...

Thanks in advance

Upvotes: 0

Views: 751

Answers (2)

Andr&#233; Figueira
Andr&#233; Figueira

Reputation: 6696

I realised that the issue was that my name spaces were wrong, so if you have the same issue just check and make sure that your namespaces are correct!

Upvotes: 0

Dale
Dale

Reputation: 10469

I'm taking a bit of a guess here but I think the issue is in the constructor..

$loader = new SplClassLoader('General', 'lib/widezike');

In the code you linked to they are the namespace and the include path.

Play around with those until it works is all I can suggest.

You might try:

$loader = new SplClassLoader(null, 'lib');

Or

$load = new SplClassLoad('General', 'lib');

On the other hand I personally just use a very simple spl_autoload_register function to do my class loading for me..

spl_autoload_register(function($class){

    $filename = str_replace('\\', '/', $class) . '.php';
    require($filename);

});

$object = new phutureproof\common\whatever();

I would then have a file /phutureproof/common/whatever.php with the contents:

<?php
namespace phutureproof\common;

class whatever
{

}

Upvotes: 1

Related Questions