PHP Class Not Found But It's Included

PHP class not found but it's included

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

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

Class not found when successfully included

When instantiating a class via a variable name you always have to include the whole namespace. And this has to be stored as a string first.

An example:

namespace Api;

Class test {

public function __construct() {
echo "test";
}
}

$className = 'test';
$classNameNamespaced = "\\Api\\".$className;

$a = new $classNameNamespaced; // echoes "test"

in your case:

$className = "\\App\\Controllers\\".$this->controller;
new $className;

PHP Class not found while it's included

@MbRostami gave the advice in a comment to use the 'get_required_files()' function to see which files are included. It turns out that the wrong files were loaded.

Root
| dashboard.php
| users.php
|- code
| Users.php
| init.php

In the init.php file the Users.php file (both files are in the code folder) was required. But for some reason the users.php file from the root was loaded. Some strange behaviour imho. Ahwell, that's something to investigate during the christmas days.

Problem is solved! Thanks!

PHP Class not found but it's in folder and it is also included

include on a relative path will search the file based on the path of the called PHP script, not the included PHP file your are calling include from.

To be able to include even if your ECommerce class is used from different locations, try

include __DIR__ . '/' . $filename;

On older PHP versions you can use dirname(__FILE__) instead of __DIR__.

There might be other dependencies before ErrorManager.php gets loaded, e.g. in Dispatcher.php.

What happens if you test the following?

include "../class/Checker.php";
include "../class/User.php";
include "../utilities/Dispatcher.php";
include "../utilities/ErrorManager.php";

Consider to use a class loading mechanism like spl_autoload_register. It makes the class dependency management much easier.

PHP namespace & use Fatal error class not found even when i already specified class with use

To do that, I advise you to use the PSR (psr-4).

First, let's create a files structure as bellow :

Sample Image

Now let's init the composer to configure the psr-4.

jump to the root of the project (in this example the root directory of src), and run :

you will be asked to fill some project information, just skip it

composer init

A file named composer.json will be created in the root directory, let's configure the psr-4 inside.

{
"name": "root/project",
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}

Learn more about psr-4

to hover, we are just telling the PSR to point the name App to the directory src and then the name of subfolder should be the surname in your namespace.

Example:

App => src directory 
App\Models => src/Models directory

And so on

Next, you should generate the autoload by

composer dump-autoload

The final project file structure seems to be something like :

Sample Image

I create a file called index.php in the root directory to test my code, but first, you should require the autoload which has been generated by the configuration we just did.

<?php  
use App\Models\City;
require __DIR__.'/vendor/autoload.php';
$city = new City();
var_dump($city);

Result:

/var/www/myfm/index.php:9:
class App\Models\City#3 (0) {
}

I hope this helps you.

PHP Fatal error: Class not found but included

I was using random_int function in PHP 5.X without the libraries required for that function. I've resolved using https://github.com/paragonie/random_compat/releases



Related Topics



Leave a reply



Submit