Wednesday, 3 June 2015

PHP: Fixed! My colleague sorts that Composer issue out for me

G'day:
This relates back to yesterday's article: "PHP: trying (but not succeeding) to create my own Github-based Composer Package". I circulated the URL to the other bods on the team, and our latest team addition Cisco had a google about (or just used his intuition, I dunno), and came up with an answer.



Basically... all we had to do was to put an autoload entry into the package's own composer.json file:

{
    "name": "dac/composertest",
    "autoload": {
        "psr-4": {"composertest\\": "src/"}
    }
}

The short version of this is that when PHP encounters a class referenced with a namespace of composertest, it knows to look in the src subdirectory of the package. Also note I've shifted the actual file into the src subdir too. This was unnecessary, but is a common practice so I ran with it.

What's curious here is that I definitely had all this working on the weekend without needing the autoload bit, so there's definitely something afoot with all this. However I'm happy enough to include this entry if it gets me moving on why I wanted to actually be able to do this in the first place, which will be a topic for another time.

Anyhow, cheers Cisco, you're a star.

--
Adam