PHP Composer PSR-4 Autoloading And Sub-Namespaces, Class Not Found - php

This question is independent but I did ask a similar question before:-
Composer Gives Error, "Class Not Found"
The problem was solved but I failed to explain the nesting issue. I thought it will be more appropriate to make a new question.
I searched a lot but I can't make the nesting namespaces to work with psr-4 autoloading.
Directory Structure:-
│ composer.json
│ run.php
│
├───src
│ ├───one
│ │ parentclass.php
│ │
│ └───two
│ childclass.php
│
└───vendor
│ autoload.php
│
└───composer
autoload_classmap.php
autoload_namespaces.php
autoload_psr4.php
autoload_real.php
ClassLoader.php
installed.json
LICENSE
parentclass.php:-
<?php
namespace myns\one;
abstract class parentclass
{
abstract public function abc();
}
childclass.php:-
namespace myns\two;
namespace myns\one;
use myns\one\parentclass as parentclass;
class childclass extends parentclass
{
public function abc()
{
echo 'hello world';
}
}
composer.json:-
{
"name": "myvendor/mypackage",
"description": "nothing",
"authors": [
{
"name": "Omar Tariq",
"email": "XXXXX#gmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"myns\\": "src/",
"myns\\one\\": "src/one/",
"myns\\two\\": "src/two/"
}
}
}
run.php:-
<?php
require_once __DIR__ . '/vendor/autoload.php';
use myns\two\childclass as childclass;
$childclass = new childclass();
$childclass->abc();
When I run php run.php. It gives error:-
Fatal error: Class 'myns\two\childclass' not found in C:\wamp\...\run.php on line 7

A class can only declare one namespace in the file. By including two namespaces in childclass.php, you are likely overriding the first.
A full example can be seen here of using multiple namespaces, but the file only includes 1 namespace declaration. That said, I suspect for your case you simply made a mistake and only need one namespace.
Because the file is located in myns\two; you should use namespace myns\two; and remove the other.

You should only add
"autoload": {
"psr-4": {
"myns\\": "src/"
}
}
The other two you added may be conflicting the namespace, because you are overriding it and tell to point to the same root /src

Related

Composer autoload classes not working

I have this kind of project structure.
├───app
│ ├───config
│ ├───controllers
│ ├───libraries
| | Controller.php
│ │ Core.php
│ │ DB.php
│ ├───models
│ └───views
└───vendor
└───composer
autoload.php
I want to load classes from libraries folder.
I have a file named bootstrap.php which is including autoload.php file and use of namespace
bootstrap.php
require_once __DIR__ . '/../vendor/autoload.php';
use Libs\DB;
use Libs\Core;
use Libs\Controller;
in the index.php file, i'm not getting any output from any class. Class names are the same as the file name and have the constructor
example DB.php
namespace Libs;
class DB {
public function __construct() {
die("Hello");
}
}
$db = new DB();
This is how composer.json looks like
{
"autoload": {
"psr-4": {
"Libs\\" : "app/libraries/"
}
}
}
and lastly my index.php file
require_once '../app/bootstrap.php';
I cannot figure out why it is not working, can you please help me, what I've been doing wrong?
This happens because you're not creating any objects of the classes you defined, so the mechanism that includes class definition file is not triggered. I guess you expected the use statements trigger this mechanism to include those files, but this is not how it works. If you try to create an object of those classes, you'll see the constructor being called. So in your index.php add this:
$db = new \Libs\DB;
$core = new \Libs\Core;
$controller = new \Libs\Controller;
to see the objects are being created correctly.

PSR-4 not loading my project classes

I'm using Composer's PSR-4 to autoload classes, but it won't load my classes.
Every time I try to use one of my classes the app dies without any error, even though it should display error.
Here is my file structure:
/
│ composer.lock
│ composer.json
│ index.php
├───src
│ Array.php
│ File.php
├───vendor
...
composer.json - autoload part
"autoload": {
"psr-4": {
"FileManager\\": "src"
}
}
Start of index.php
<?php
require(__DIR__ . '/vendor/autoload.php');
var_dump(class_exists('Array'), class_exists('Array', false));
var_dump(class_exists('File'), class_exists('File', false));
And it dumps this:
bool(false)
bool(false)
bool(false)
bool(false)
If I add
use FileManager\Array;
it will die instantly. If I add
use FileManager\File;
it won't die, but it won't recognize File class either.
I have ran
$ composer dumpautoload
Can somebody help me with this?
The problem is that you are passing strings to class_exists().
If you use string values to reference class names, you will need to use the fully-qualified class name.
<?php
require_once __DIR__ . '/vendor/autoload.php';
var_dump(
class_exists('FileManager\ArrayUtil'),
class_exists('FileManager\File')
);
Alternatively, you can use the class keyword (requires at least PHP 5.5):
<?php
use FileManager\ArrayUtil;
use FileManager\File;
require_once __DIR__ . '/vendor/autoload.php';
var_dump(
class_exists(ArrayUtil::class),
class_exists(File::class)
);
For reference, see:
http://php.net/manual/en/language.namespaces.dynamic.php
http://php.net/manual/de/migration55.new-features.php#migration55.new-features.class-name
http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.class

Composer PSR-4 PHPUnit class not found in same directory

I have the following project structure
src/
├─ MyPackage/
├─ MySDK.php
└─ SDKHelper.php
test/
├─ MySDKTest.php
└─ TestUtils.php
composer.json
composer.lock
My composer.json file looks like this:
"autoload": {
"psr-4": {
"MyPackage\\": "src/MyPackage"
}
}
Everything worked great, and MySDKTest.php unit tests were passing, until I tried adding some utility methods to a third file, TestUtils.php. When I try to call TestUtils::utilityMethod() from MySDKTest.php, phpunit complains that the class TestUtils is not found.
I've been reading about adding an autoload-dev key, variations whereof I have tried, but so far, it appears that nothing is working. I should clarify that I am able to use MySDK and SDKHelper methods inside MySDKTest. MySDKTest.php looks like this when it works:
use MyPackage\MySDK;
class MySDKTest extends PHPUnit_Framework_TestCase {
public function testPackage() {
$sdk = new MySDK();
$sdk->exampleMethod();
}
}
It should be pretty simple. Composer's PSR-4 autoloader just defines a mapping from a namespace to a folder.
Are your tests namespaced correctly? It looks like they're not since you have a use at the top of your test class. If MySDK is in the namepace MyPackage (fully qualified MyPackage\MySDK), I would expect MySDKTest to also be in the MyPackage namespace at MyPackage\MySDKTest. It doesn't have to be that way - you could put the tests in a different namespace if you prefer.
Regardless, the reason it's not working is you didn't register the test folder with the autoloader. The way it looks like your stuff is currently set up, your autoloader config should look like this:
{
"autoload": {
"psr-4": { "MyPackage\\": ["src/MyPackage/", "test/"] }
}
}
Also you'd need to change use MyPackage\MySDK; to namespace MyPackage; in your test class.
Note
Your folder structure is a little weird. I would expect test to match src. So it would be like this:
test/
├─ MyPackage/
├─ MySDK.php
└─ SDKHelper.php
Adjust the namespace accordingly:
{
"autoload": {
"psr-4": { "MyPackage\\": ["src/MyPackage", "test/MyPackage"] }
}
}

Composer PSR-0 autoloading dont work

I have problem with autoloading module controller class from my app. I try to configure composer.json but still not working. Probably am doing somthing wrong with this directory structure. I try examples from composer doc but again dont work...
Directory structure:
|- admin
|----- modules
|--------- Menu
|------------Controller
|--------------MenuController.php
Lets see composer.json
{
"autoload": {
"psr-0": { "Admin\\Modules\\": "" }
}
}
I try to set path but nothing again // "Admin\Modules\": "admin/modules"
Menu controller:
namespace Admin\Modules\Menu\Controller;
class MenuController extends AbstractAdminBaseController
FrontController
require 'vendor/autoload.php';
new \Admin\Modules\Menu\Controller\MenuController();
All time Class not found. I try 100 examples from google and nothing. Any example how to slowe this problem? Thanks
Update structure:
├───admin
│ └───modules
│ └───Menu
│ └───controller
│ └───MenuController.php
├───vednor
│ └───autoload.php
│ └───composer
│ └───autoload_classmap.php
│ └───autoload_namespaces.php
│ └───autoload_psr4.php
│ └───autoload_real.php
│ └───ClassLoader.php
├───public
├───assets
├───index.php
├───composer.json
├───composer.lock
Your PSR-0 will never work, because this standard dictates that the path to the file has to be EXACTLY like the classname. Note that your first part of the namespace is "Admin", but the first directory part is only "admin" - cases must match exactly, or it won't work (or will only work with case insensitive filesystems).
You will succeed with using PSR-4, though. Why? Because with PSR-4, the given namespace prefix is removed from the full classname, and the rest is being converted into a path that is searched in the directory given for the namespace prefix.
Example for your case:
"autoload": {
"psr-0": { "Admin\\Modules\\": "" }
}
Will not work because the files are in path admin/modules, but must be in Admin/Modules.
"autoload": {
"psr-4": { "Admin\\Modules\\": "admin/modules/" }
}
Will work because the prefix Admin\Modules\ is removed and the remaining class name is being converted to a path and added to admin/modules.
Ah, one gotcha! It will NOT work, because you chose to name the class ...\Controller\..., but the path once again .../controller/....
Honestly, I'd highly recommend to convert your file names and location to PSR-4 compatibility, even for the prefixed directories that you'd be able to work around with Composer. This will eliminate the surprising lowercase directory structures I see.
I mean: Why is that controller directory even lower case in the first place if every class located there is Controller? I really can't understand this.

Composer psr-0 autoloading of custom namespaces does not work

I have trouble adding my own namespaces to composer with PSR-0. I have read this and this but I still can't make it work
composer.json
{
"require": {
"klein/klein": "2.0.x",
"doctrine/orm": "2.4.4"
},
"autoload": {
"psr-0": {
"mynamespace": "src/"
}
}
}
The src folder is placed inside the same directory as composer.json
The src directory has the following structure
src
└── mynamespace
├── Keys.php
Keys.php
<?php
namespace mynamespace\Keys;
define ("API_KEY", "XXXXXXXXXXXX");
?>
index.php
use Klein\Klein;
use mynamespace\Keys;
require_once __DIR__ . '/vendor/autoload.php';
$klein = new Klein(); // works
echo API_KEY; // Undefined constant
You can only load classes, interfaces and traits with autoloading.
Because all you do is add a use clause which does not do anything by itself with autoloading (i.e. it does not load something), and you do not use a class, nothing happens.
I recommend using class constants. They may be put into classes or interfaces:
namespace mynamespace;
interface Keys {
const API_KEY = 'XXXXXXXX';
}
use mynamespace/Keys;
echo Keys::API_KEY;

Categories