Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Last active August 29, 2015 14:04
Show Gist options
  • Save jasonrhodes/727c097bf5467b039063 to your computer and use it in GitHub Desktop.
Save jasonrhodes/727c097bf5467b039063 to your computer and use it in GitHub Desktop.
Why PSR-0/4 are confusing, from my perspective

PSR-0 and PSR-4 are both PSR-Confusing

Phil Sturgeon asked everyone's opinion about deprecating PSR-0 and I was an ass and said they were both awful. I don't think that, but I think they're both about equally confusing from userland so it doesn't really matter to me. Phil had a great question https://twitter.com/philsturgeon/status/490659076096131072 about how they're confusing, and I needed more than a few tweets to explain.

I use PSR-0 and now some PSR-4 all the time for autoloading, although my experience is generally filling out composer.json "autoload" keys. Every single time I go to fill out "autoload": { "psr-0": I have to spend 5-10 minutes going back through docs trying to figure out how to designate the right path -> namespace match so that my files will load. In other words, neither is very intuitive. "What folders do I need to nest inside src? What do I have to repeat? What if I want to just include some util classes in my lib folder?"

I always assume I can just declare

{
  "autoload": {
    "lib/utils": "MyUtils"
  }
}

and get lib/utils/Thingy.php === <?php namespace MyUtils; class Thingy { and for some reason that never seems to work in either 0 or 4, and it's hard for me to ever remember why.

Compare that to the other language I write every day, JS in the node/commonJS format:

var corelib = require("core-lib");
var third = require("third-party-lib");
var myutil = require("./lib/myutil");

For whatever reason, this is just really straightforward. I never have to look anything up about this in any docs ever because it maps to how my brain thinks about the way files might be loaded--either you know where they are by default and I just require them by name, or I tell you where they are explicitly from where I'm requiring them. Sometimes I wonder if just going back to PHP require "./path/to/mylib" would be clearer and easier to reason about...

But anyway, I do think that autoloading in PHP has come a long way and that both 0 and 4 are solid standardizations that do the job they're trying to do while maintaining some tricky backwards compatibility for big frameworks that need it, but I just don't feel like one or the other is obviously better from a usability perspective. (Obviously there are probably many other perspectives to consider, I realize.)

@philsturgeon
Copy link

The trouble here, is that a few different things are being confused.

{
  "autoload": {
    "lib/utils": "MyUtils"
  }
}

This looks like an attempt at writing a composer.json. Is that what you intended? Because if so, that has nothing to do with the PSR-0 or PSR-4 standard itself, right? Composer happens to use the rules, but is not part of it. They're often confused.

var corelib = require("core-lib");

Absolutely, some sort of package loading system in PHP would be amazing, much like Python or anything else.

This is "fault", or a lack of a feature of PHP itself. It is not something that the PHP-FIG have control over, but is something I'd certainly love to see some day.

If PSR-0 and PSR-4 are confusing based on this, then it is not really the fault of the specifications themselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment