[php] PHP class not found but it's included

I'm including a PHP class with

require_once($ENGINE."/classUser.php");

but when the code is executed i receive this error:

Fatal error: Class 'User' not found in C:\xampp\htdocs\WebName\resources\engine\ajax\signup.php on line 12

I still can't figure out what's the problem. I'm 99% sure it's correct.

The "$ENGINE" is correct, and the class is correct too (Netbeans suggests me class methods and variables).

signup.php:

<?php

/* Created on: 13/12/2011
 * Author: 
 * 
 * Description: User signup procedure.
 */

require_once("../settings.php");
require_once($ENGINE."/classUser.php");

$user = new User();
$user->createUser($_POST["username"], $_POST["email"], $_POST["password"]);


?>

classUser.php:

<?php

/* Created on: 13/12/2011
 * Author: 
 * 
 * Description: This class manages users.
 */

require_once("settings.php");
require_once($LIBRARY."/cassandraphp/cassandra.php");

class User {

    public function createUser($username, $email, $password){
        $cassandra = Cassandra::createInstance($CASSANDRASERVER);
        $cassandra->set(
                "user.".$username,
                array(
                    'ID' => uniqid(),
                    'Username' => $username,
                    'Email' => $email,
                    'Password' => $password
                )
        );
    } 
}

?>

This question is related to php

The answer is


As a more systematic and structured solution you could define folders where your classes are stored and create an autoloader (__autoload()) which will search the class files in defined places:

require_once("../settings.php");
define('DIR_CLASSES', '/path/to/the/classes/folder/'); // this can be inside your settings.php

$user = new User();
$user->createUser($_POST["username"], $_POST["email"], $_POST["password"]);

function __autoload($classname) { 
    if(file_exists(DIR_CLASSES . 'class' . $classname . '.php')) {
        include_once(DIR_CLASSES . 'class' . $classname . '.php'); // looking for the class in the project's classes folder
    } else {
        include_once($classname . '.php'); // looking for the class in include_path
    }
} 

Maybe it is how you use new User(). Set path something like

$user = new \resources\engine\ajax\User();

Double check your autoloader's requirements & namespaces.

  • For example, does your autoloader require your namespace to match the folder structure of where the file is located? If so, make sure they match.
  • Another example, does your autoloader require your filenames to follow a certain pattern/is it case sensitive? If so, make sure the filename follows the correct pattern.
  • And of course if the class is in a namespace make sure to include it properly with a fully qualified class name (/Path/ClassName) or with a use statement at the top of your file.

  1. Check File Permissions
  2. Check File size.

Sometimes an inaccessible or corrupted file would be the problem, as was in my case


Check your file permissions for the correct linux user for classUser.php


After nearly two hours of debugging I have concluded that the best solution to this is to give the file name a different name to the class that it contains. For Example:

Before example.php contains:

<?php
 class example {

 }

Solution: rename the file to example.class.php (or something like that), or rename the class to example_class (or something like that)

Hope this helps.


My fail might be useful to someone, so I thought I would post as I finally figured out what the issue was. I was autoloading classes like this:

define("PROJECT_PATH", __DIR__);

// Autoload class definitions
function my_autoload($class) {
    if(preg_match('/\A\w+\Z/', $class)) {
        include(PROJECT_PATH . '/classes/' . $class . '.class.php');
    }
}
spl_autoload_register('my_autoload');

In my /classes folder I had 4 classes:

dbobject.class.php
meeting.class.php
session.class.php
user.class.php

When I later created a new class called:

cscmeeting.class.php

I started getting the can't load DbObject class. I simply could not figure out what was wrong. As soon as I deleted cscmeeting.class.php from the directory, it worked again.

I finally realized that it was looping through the directory alphabetically and prior to cscmeeting.class.php the first class that got loaded was cscmeeting.class.php since it started with D. But when I add the new class, which starts with C it would load that first and it extended the DbObject class. So it chocked every time.

I ended up naming my DbObject class to _dbobject.class.php and it always loads that first.

I realize my naming conventions are probably not great and that's why I was having issues. But I'm new to OOP so doing my best.


you should declare namespace in the ClassUser.php, something like this:

<?php
namespace app; // where 'app' is a folder declared as a root for the project
class ClassUser{
public function test(){
//log something here
}
}
?>

Then you can add the class in your other php files like this:

<?php
use app\ClassUser;
$classUserLcl = new ClassUser();
$classUserLcl->test();
?>

and you are done. Otherwize it will abuse:

You Oh! its a Fatal error : Uncaught Error: Class 'app\ClassUser' not found in ...


It 'happened to me! The problem is that somehow you include a file with the same file name of the class thus invalidating the same class!

Check the path of inclusion and these checks files with the same name!


Check to make sure your environment isn't being picky about your opening tags. My configuration requires:

<?php

If i try to use:

<?

Then I get the same error as you.


The problem went away when I did

sudo service apache2 restart

if ( ! class_exists('User')) 
    die('There is no hope!');

I had this problem and the solution was namespaces. The included file was included in its own namespace. Obvious thing, easy to overlook.


It may also be, that you by mistake commented out such a line like require_once __DIR__.'/../vendor/autoload.php'; --- your namespaces are not loaded.

Or you forget to add a classmap to the composer, thus classes are not autoloaded and are not available. For example,

"autoload": {
    "psr-4": {
        "": "src/"
    },
    "classmap": [
        "dir/YourClass.php",
    ]
},
"require": {
    "php": ">=5.3.9",
    "symfony/symfony": "2.8.*",

If you've included the file correctly and file exists and still getting error:

Then make sure:
Your included file contains the class and is not defined within any namespace.
If the class is in a namespace then:
instead of new YourClass() you've to do new YourNamespace\YourClass()


In fact, it's a very old thread but useful...

When namespace declaration is part of your php class file "this kind of weird errors tends to appear".

Solution: Use namespace with {, your code shows like this:

<?php

namespace path_to\lib {

require_once "folder/php_class_file_where_namespace_declaration_is_part_of_it.php";

**YOUR CODE HERE**

<?php } ?>