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 :
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 initA 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-4to 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 onNext, you should generate the autoload by
composer dump-autoload
The final project file structure seems to be something like :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
Phpmailer - Mails Going Straight to Spam
Where to Store Database Login Credentials for a PHP Application
PHP Dom Textcontent VS Nodevalue
Posting to a Facebook Page as the Page (Not a Person)
Create a Joomla! Article Programmatically
Compare Object Properties and Show Diff in PHP
Force Logout Users If Users Are Inactive for a Certain Period of Time
Capital Letters in Class Name PHP
Difference Between Buffered and Unbuffered Queries
How to Call Curl_Setopt with Curlopt_Httpheader Multiple Times to Set Multiple Headers
How to Force Ssl in Codeigniter
How to Split a String into an Array of Unicode Characters in PHP
Sending JavaScript Object to PHP via Ajax
How to Watch a File Write in PHP
Why Doesn't PHP Permit Private Const
PHP - Ini_Set('Session.Gc_Maxlifetime', 5) - Why It Doesn't End the Session