PHP Class Not Found When Using Namespace

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 the bootstrap.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 :

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.

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



Leave a reply



Submit