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
Related
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.
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
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.
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;
I'm currently working on a Symfony2 project with Phpspec and I'm having problems to extend a Spec class described in a different namespace.
In my project, for instance, I'm having the following class described in spec/Acme/Model/Foo/FooSpec.php :
namespace spec\Acme\Model\Foo;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
abstract class FooSpec extends ObjectBehavior{
//some code here
}
And I have another class in spec/Acme/Model/Bar/BarSpec.php extending FooSpec :
namespace spec\Acme\Model\Bar;
use spec\Acme\Model\Foo\FooSpec;
class BarSpec extends FooSpec{
//some code here
}
When I try to run phpspec, I have the following error :
PHP Fatal error: Class 'spec\Acme\Model\Foo\FooSpec' not found in /home/user/Projects/Acme/spec/Acme/Model/Bar/BarSpec.php on line 9
The only way I found to make it work was to add the following line in spec/Acme/Model/Bar/BarSpec.php:
include('./spec/Acme/Model/Foo/FooSpec.php');
I don't know why I have to include this specific file to make it run, especially when the other classes (like PhpSpec\ObjectBehavior) are correctly found.
Do you have any idea why is this happening?
Edit:
As suggested by #Phil and #Sheikh Heera in the comments, I tried to set up an autoload to register my spec namespace but it's not working neither. Here is what I tried so far :
require_once getcwd() . '/vendor/composer/ClassLoader.php';
$loader = new \Composer\Autoload\ClassLoader();
// register classes with namespaces
$loader->add('spec', getcwd().'/spec');
// activate the autoloader
$loader->register();
I also tried to modify the file vendor/composer/autoload_namespaces.php to add this :
return array(
//some code here
'spec' => array(getcwd() . '/spec'),
//some more code
);
But still the same error. I also tried with 'spec' => array(getcwd()) or $loader->add('spec', getcwd()); just to see what will happen and this time I get a Cannot redeclare class on another spec class.
My php version is PHP 5.4.9-4ubuntu2.4 (cli).
Thank you in advance for your help.
A more generic solution is to put the spec directory into the autoload-dev configuration of your composer.json:
"autoload-dev": {
"psr-0": {
"spec\\":""
}
},
This way, composer will generate the namespace also for the specs, which should be on the root of the repository:
return array(
'spec\\' => array($baseDir . '/'),
...
);
Finally, what I actually did to "solve" my "problem", as suggested by Phil and Sheikh Heera in the comments, is to autoload my namespace so it is recognized by spec.
Here is what I did to make it work :
I added the following line to the file `vendor/composer/autoload_namespaces.php` :
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
//some code
'spec\\Acme\\Behavior' => array(getcwd()),
//some code
);
Then I created the following folder structure :
│── spec
│ └── Acme
│ └── Behavior
│ └── FooBehavior.php
I declared the namespace in `spec/Acme/Behavior/FooBehavior.php` this way :
namespace spec\Acme\Behavior;
And used it in `spec/Acme/Model/Bar/BarSpec.php` as following :
use spec\Acme\Behavior\FooBehavior;
class BarSpec extends FooBehavior{
//...
}
I know this is not the best practice because everytime someone will be working on this project it will have to reproduce manually the first step. So if you have any better idea, please feel free to comment or post an answer.