PHP class not found but it's included

Php

Php Problem Overview


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
                )
        );
    } 
}

?>

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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.*",

Solution 5 - Php

First of all check if $ENGINE."/classUser.php" is a valid name of existing file. Try this:

var_dump(file_exists($ENGINE."/classUser.php"));

Solution 6 - Php

The problem went away when I did

sudo service apache2 restart

Solution 7 - Php

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
    }
} 

Solution 8 - Php

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!

Solution 9 - Php

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

Solution 10 - Php

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 } ?>

Solution 11 - Php

  1. Check File Permissions
  2. Check File size.

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

Solution 12 - Php

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 ...

Solution 13 - Php

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.

Solution 14 - 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.

Solution 15 - Php

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.

Solution 16 - Php

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

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

Solution 17 - Php

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()

Solution 18 - Php

Yes this happen in cases when use a trait or extend a class, you should be aware to instantiate the class after the class declaration for example:

This example will trigger class not found error:

$class = new A();

class A {
   use SomeTrait;
}

To make it to work move the initialization step in the bottom like so:

class A {
   use SomeTrait;
}

$class = new A();

Solution 19 - Php

I found I had to use the use keyword, as well as the include statement. Tested it with either missing, and it doesn't work.

namespace foo;
use src\config\object;
include('config/object.php');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsiannoneView Question on Stackoverflow
Solution 1 - PhpjarederajView Answer on Stackoverflow
Solution 2 - PhpDejan MarjanovićView Answer on Stackoverflow
Solution 3 - PhpRaiden616View Answer on Stackoverflow
Solution 4 - PhpolgaView Answer on Stackoverflow
Solution 5 - PhpMinrasView Answer on Stackoverflow
Solution 6 - PhpRussell EnglandView Answer on Stackoverflow
Solution 7 - PhpMinrasView Answer on Stackoverflow
Solution 8 - PhpLorenzo MagonView Answer on Stackoverflow
Solution 9 - PhpAndrewView Answer on Stackoverflow
Solution 10 - PhpEnrique MendozaView Answer on Stackoverflow
Solution 11 - PhpCedric IpkissView Answer on Stackoverflow
Solution 12 - PhpCodeToLifeView Answer on Stackoverflow
Solution 13 - PhpJeff SolomonView Answer on Stackoverflow
Solution 14 - PhpJames GibbonsView Answer on Stackoverflow
Solution 15 - PhpAndrewView Answer on Stackoverflow
Solution 16 - PhpAlf Georg ØstebrøtView Answer on Stackoverflow
Solution 17 - PhpskmakView Answer on Stackoverflow
Solution 18 - PhpWael SalahView Answer on Stackoverflow
Solution 19 - PhpSpinstazView Answer on Stackoverflow