Cannot find Class with PHP Namespace
Even when using use
statement, you need to specify the namespace of the class you are trying to instantiate. There are a lot of examples here: http://www.php.net/manual/en/language.namespaces.importing.php
To understand it better, I will describe to you how it works. In your case, when you do use \Controller
, the whole Controller
namespace becomes available to you, but not the classes that are in this namespace. So, for example:
<?php
include('testcontroller.php');
use \Controller;
// Desired class is in namespace!
$controller = new Controller\Controller();
// Error, because in current scope there is no such class
$controller = new Controller();
$controller->show();
?>
Another example:
testcontoller.php:
<?php
namespace Some\Path\To\Controller;
class Controller
{
function __construct()
{
}
function show()
{
echo '<br>Was run inside testcontroller.php<br>';
}
}
?>
testing.php:
<?php
include('testcontroller.php');
use \Some\Path\To\Controller;
// We now can access Controller using only Controller namespace,
// not Some\Path\To\Controller
$controller = new Controller\Controller();
// Error, because, again, in current scope there is no such class
$controller = new Controller();
$controller->show();
?>
If you wish to import exactly the Controller
class, you need to do use Controller\Controller
- then this class will be accessible in your current scope.
Class not found when using namespace
Your autoloader is not working because WordPress
will never activate it;
Importing it in your fahad-plugin.php
file should fix it, like below:
<?php
/**
* @wordpress-plugin
* Plugin Name: Fahad-plugin
* Description: Your plugin description
* Version: 1.0
* Author: Fahad Shaikh
* Author URI: https://example.com
*/
require_once __DIR__ . '/vendor/autoload.php';
use inc\Activate;
class FahadPlugin {
function activate() {
Activate::activate();
}
}
$fp = new FahadPlugin();
register_activation_hook(__FILE__, array($fp, 'activate'));
Note:
WordPress
loads thebootstrap.php
file if there is no file with the same name as your plugin.
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 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 :
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.
Class not found error using namespaces and use
As you are not using autoloading, files in directory other than Controllers will not load.
So if you do not want to use autoloading, you should either use include statement or require statement to include the files manually(files which are in other directory).(Include file Post.php in PostController.php file)
As mentioned in your comments, other files which are working must be getting loaded because they exist in the same directory(same package does not matters here but same directory matters for other files to get loaded).
PHP classes in namespaces not found
It looks like the pattern in glob
is relative to the working directory, you may use a determined path.
glob(dirname(__DIR__) . "/Classes/*Utility.php")
Class not found using Namespaces and Autoloader
Although in comment we already solved this problem, i decided to write an answer for this.
The question already have the right file path, namespacing, and composer.json configuration. The only one missing is generating the autoload scripts using composer dump-autoload
.
This command will make several autoload*.php
files that contains file path paired with its namespace. As explained in Composer Website.
Related Topics
Converting a Carbon Date to MySQL Timestamp
How to Access the Google Spreadsheets API in PHP
Simplexml: Append One Tree to Another
PHP Domdocument - Get HTML Source of Body
PHP JSON_Encode and JavaScript Functions
Adjusting Time Zone in PHP with Datetime/Datetimezone
PHP Class Not Found When Using Namespace
Date Function to Display All Dates Between Two Dates
Restrict Content Access to Logged in Users with PHP
Phpdoc for Variable-Length Arrays of Arguments
Laravel Get Route Name from Given Url
Shell_Exec() Timeout Management & Exec()
Facebook Oauthexception: "User Hasn't Authorized the Application to Perform This Action"
Need to Merge Multiple PDF's into a Single PDF with Table of Contents Sections
Un-Encrypting/Re-Encrypting a Coldfusion Encrypted String in PHP
Wordpress, Nginx Proxy and Subdirectory: Wp-Login.PHP Redirects to Domain