views:

36

answers:

4

Here's what the class loader looks like:

loader.php

class Load
{
    public static function init($class)
    {
        require_once 'classes/' . $class . '.php';
        return new $class();
    }
}

$class = Load::init('MyClass');

The error being returned is:

Fatal error:  Class 'MyClass' not found in /www/website/application/models/Database/loader.php on line 5

If I put an echo 'WORKS'; into MyClass.php, I can see that it isn't being included. That echo doesn't execute.

UPDATE: It seems I've been editing a cached version of my code and not the actual file... The code works :}

+2  A: 

Try this.

function __autoload($class_name) {
    include $class_name . '.php';
}

Put that anywhere publically accessable to the rest of your code. At that point, when you do something like this.

$class = new Foobar();

If the class is not already included, php will run that __autoload function and include the file for you. Make sure you point that include statement to wherever your classes are stored.

For more information on that, check out the php doc here.

http://php.net/manual/en/language.oop5.autoload.php

castis
The problem with me using that is I'll come back to the code in a few weeks and think to myself "where is that class being included... :/". By using `Load::init('MyClass');`, I can see where the classes are being loaded from.
charles
If you're using the Zend Framework, its auto loader should already be registered with spl_autoload_register
Phil Brown
charles, make sure that the path to MyClass.php is clear. It looks like you're telling it to look in /www/website/application/models/Database/classes. I just tried that code example out and it worked out okay for me.
castis
A: 

Does classes/MyClass.php contain a class definition for MyClass?

I'm assuming the require was able to find classes/MyClass.php on the include path, but is it loading the correct file?

Phil Brown
Yeah, it has the `class MyClass { }`. Given that my `echo` isn't being executed when I try to `require_once` the class, my guess is that it's not being included. However, no error is produced saying it couldn't find/include it :/
charles
Are there any other `MyClass.php` files that reside in a `classes` directory anywhere else in your project or on your include path?
Phil Brown
I see from your update that this is indeed the case.
Phil Brown
A: 

Could be a couple things...

  • What is the filesystem path to classes/? Is it on your include path? If not, you need to use the absolute path.

  • If youre on *nix, most of them use a case sensitive filesystem - is you file named MyClass.php or myclass.php?

prodigitalson
I've set the `require_once` to use an hard-coded path which is the absolute system path. The file is called `MyClass.php`. The `require_once` isn't producing an error so it has to be including it... I'm just unable to access it's namepsace :/
charles
Namespace eh? What is the namespace of `MyClass`? You're probably not including the appropriate `use` statement if you class is namespaced.
Phil Brown
Unless its a typo in your question `classes/MyClass.php` IS NOT an absolute path. Thats a relative path synonymous with `./classes/MyClass.php` where `.` is the path to the calling class - in this case `/www/website/application/models/Database/`. An absolute path would look like `/classes/MyClass.php`.
prodigitalson
@phil: I think he mean the namespace as it relates to PEAR class loading style, not the actual namespace feature in >= 5.3...
prodigitalson
Actually all this path stuff may be moot, as Phil pointed out in his comment directly to your question `require` should produce a different fatal error if it cant find the file.
prodigitalson
A: 

Three tips:

  1. You should use Zend_Autoloader or subclass one of the Zend's autoloaders if you are using Zend Framework. To use Zend Autoloader you need to configure autoloader namespace, e.g. in application.ini and name/place your files according to PEAR (Zend) naming convention.
  2. You should use absolute path, because relative paths may vary and point to other resources, e.g. APPLICATION_PATH . '/../your/path'
  3. Check the file permissions/ownership
takeshin