Questions tagged [psr-4]

PSR-4 describes requirements that must be adhered to for autoloader interoperability in PHP. PSR-4 is an alternative to PSR-0 that corrects problems discovered in real-world use.

Specification link:

Code and test samples:

308 questions
5
votes
2 answers

PHP Namespace Class Naming Convention

I currently follow PSR-2 and PSR-4. I'm running into a small dilemma when trying to name a few classes. Here's an example. I have a base REST client, \Vendor\RestClient\AbstractClient. I have two implementations of this Abstract…
5
votes
1 answer

How do I autoload classes that have a file name different from the class name?

I have seen these, How to autoload class with a different filename? PHP Load a class with a different name than the one passed to the autoloader as argument I can change but in my MV* structure I have: /models customer.class.php …
johnny
  • 19,272
  • 52
  • 157
  • 259
5
votes
2 answers

PHP composer autoload not loading class

I'm just getting started using composer for dependency management and I'm having a hard time figuring out how I'm not adhering to psr-4 for autoloading so I'm here for advice. I've got a class that generates random values that is already on the…
ejfrancis
  • 2,925
  • 4
  • 26
  • 42
5
votes
1 answer

Composer - Autoload and PSR-0 vs PSR-4

I'm beginning to study Composer and am developing a system where I separate the files core application files, as follows: /root |-- /src |-- /App |-- /DBConfig |-- /Controller |--…
LeoFelipe
  • 281
  • 6
  • 13
5
votes
1 answer

Does PHP's use statement cause extra work when loading classes?

Code Sample 1 use Outline\Drawing; $var = new Drawing(); Code Sample 2 $var = new Outline\Drawing(); Question: Does PHP make hardware work harder (look up more files or do more processing) if I use code in sample 1? I am sure something gets done,…
Dennis
  • 7,907
  • 11
  • 65
  • 115
5
votes
1 answer

Can't generate entities after psr-0 to psr-4 change

I changed the psr-0 autoloading to psr-4 for a few of my bundles. Now, when I try to generate entities with the app/console command, it gives me Can't find base path for "MbDbFooBundle" (path: …
rolandow
  • 1,049
  • 1
  • 15
  • 28
4
votes
2 answers

How to make vendor package classes being extendable with PSR-4 autoloading?

I'm using Laravel as framework to build an application framework which can be used to build their own application by fellow developers. Now I'm running into a little PSR-4 namespacing problem in the composer packages I'm developing. Example of how…
Erwin Augustijn
  • 101
  • 3
  • 10
4
votes
1 answer

How to indent a long ternary condition in Php?

I have a long ternary condition in Php that exceeds the 120 hard rule. What would be the right indentation according to PSR standard? This is my ternary condition. The condition is a bit long, so the function call exceeds the 120 rule. $variable =…
Shylajhaa
  • 1,418
  • 2
  • 11
  • 20
4
votes
1 answer

Generating optimized autoloader automatically

I'm working on a simple project using PHP and I'm using PSR-4 autoloading. I just wonder if there's a way to run composer dump-autoload -o on composer install, so that new users won't need to type composer dump-autoload -o to avoid autoloading…
21 Century
  • 43
  • 1
  • 6
4
votes
4 answers

Composer psr-4 autoload issue

I have problem with autoloading with composer when i use psr-4 autoloading it doesn't work and give me error. I tried: $ composer dump-autoload and a lot of other thing but it doesn't work without require one; error: You are now a master…
Václav Stummer
  • 349
  • 2
  • 8
  • 21
4
votes
1 answer

composer autoload file after classmaps are loaded

I have "autoload": { "psr-4": { "ACME": "src/", }, "classmap": ["src/"], "files": ["mapper.php"], "exclude-from-classmap": ["mapper.php"] }, in mapper.php I'm trying to give different namespaces for some…
Thellimist
  • 3,757
  • 5
  • 31
  • 49
4
votes
1 answer

Using PSR4-Autoloading via Composer vs spl_autoload_register

We're building a new Slim application and we want to use PSR4-Autoloading with namespaces in our code. We found two ways to do that, via Composer i.e.: "autoload": { "psr-4": { "App\\Controller\\": "app/controllers", …
Naguib Ihab
  • 4,259
  • 7
  • 44
  • 80
4
votes
2 answers

PHP reserved keywords allowed in namespace? (public, private, default)

PhpStorm is highlighting the following namespace as an error.
MrMAG
  • 1,194
  • 1
  • 11
  • 34
4
votes
1 answer

PSR-4 namespace conventions for folders on same level as src

The current convention for PSR-4 is to namespace the src folder as Vendor\Package. Any file within is then namespaced using the directory structure. so src/Model/MyModel.php uses namespace Vendor\Package\Model; class MyModel {...} This is…
myol
  • 8,857
  • 19
  • 82
  • 143
4
votes
1 answer

Understanding PSR-4 Composer without any framework

I try to understand and learn how it works PSR-4, because I like refactor with this standard, a little app. According several manuals, and post on stack overflow, my structure and files are ok. /vendor /abkrim /rclub /src/ …
abkrim
  • 3,512
  • 7
  • 43
  • 69
1 2
3
20 21