Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Autoloader issue: File does not contain class error
  • I am using a fresh install of FuelPHP 1.7.3 and i'm getting the following error when trying to call a method of a custom class:


    Exception [ Error ]:
    File "APPPATH/classes/testclass.php" does not contain class "Testclass"

    COREPATH/classes/autoloader.php @ line 395

    The contents of APPPATH/classes/testclass.php :


    <?php

    class Testclass {

    function __construct() {

    }

    public static function test() {

    return 'HELLO';

    }

    public static function _init() {

    }

    }

    and in the APPPATH/bootstrap.php file i have:


    ....
    // Initialize the framework with the config file.
    \Fuel::init('config.php');

    echo Testclass::test();

    Removing the __construct and _init methods from testclass.php makes no difference.


    APPPATH is also pointing to the right path on the filesystem too.


    Not sure why Fuel can't find the class?

  • You get this if in the autoloader

    if (class_exists($class, false))

    returns false, and the only reason for this happening, assuming the file itself exists and is ok, is a rights issue. Can your webserver user read the file?
  • The user and group the file is owned by is the same as all other PHP files on all other sites on the server so i don't think it's that, i've checked the permissions on the classes/testclass.php file and the owner and group have read and write access, just to make sure i chmod 777 the file so all users have read / write access, makes no difference though :(


    if i var_dump the code above class_exists does return false however i am var_dump'ing it before the echo Testclass::test(); line. I added the following line of code above "echo Testclass::test();" to make sure the file was being included:


    include('classes/testclass.php');
    echo Testclass::test();

    and i get the following error:

    ErrorException [ Fatal Error ]:
    Cannot redeclare class Testclass

    APPPATH/classes/testclass.php @ line 3

    Fuel must be including the file at some point to get this error but the autoloader isn't picking up on the class after it's included perhaps?

  • *face palm* i fixed the issue. It was a typo when declaring the class. My bad, everything is working now :D
  • HarroHarro
    Accepted Answer
    Ok, cool.

    To answer your last question: fuel doesn't know you've included it, so when you call the test method, the autoloader includes it again, causing that error.
  • Ah i see, makes sense. When you first make a reference to a class how does the autoloader know if it's been included / referenced already or not?
  • The autoloader contains a cache of loaded classes. When it's not in the cache, it includes the file it thinks the class is in, and then uses class_exists() to verify if the file included does actually contain the class.
  • Another possible reason for getting this is if you're running an Oil command from the CLI and your class uses PHP short tags - ie. <? – rather than the full <?php – PHP may not be setup to parse these correctly. 

    The clue is the full contents of the file appear in the terminal, but you may not notice if your eyes focus on the error message and callstack at the bottom.   Fixable by editing the file (obviously) or setting short_open_tag to On in your CLI php.ini (e.g. /etc/php/7.0/cli/php.ini)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion