Dal
Dal

Reputation: 83

PHP defining namespaces on composer's vendor packages

I'm having some issues with namespaces in PHP.

I've got everything nailed down and working within my project which is all running great. I used composer's autoload features to autoload all my project classes.

Recently I've needed to pull in a dependency through composer for securimage (captcha application). The problem I've got is that it won't work until I go editing the files and insert the following at the top of each php script.:

<?php namespace vendor\dapphp\securimage;

My composer.json file is using PSR-4 if that helps identify where I'm going wrong.

"psr-4": {
            "vendor\\dapphp\\securimage\\": "vendor/dapphp/securimage"
        }

My question in case it isn't obvious how do I pull in composer vendor projects and make PHP automatically insert/understand that these should be placed under the namespace

vendor\{userid}\{projectid}

without editing the actual files within.

I'm sure I've just missed something within the composer.json file?

Upvotes: 3

Views: 4015

Answers (1)

deceze
deceze

Reputation: 522033

You haven't really missed anything. It's up to a package to namespace its source code. Unless the source code literally contains a namespace declaration, the code is in the global namespace. You cannot change that fact without changing all the involved source code. The Securimage code is not namespaced, period.

"psr-4": {
    "vendor\\dapphp\\securimage\\": "vendor/dapphp/securimage"
}

This merely configures the autoloader. Meaning, should you try to load a class in the namespace vendor\dapphp\securimage\..., Composer's autoloader knows where to find it. It does not place the code in this namespace.

Upvotes: 2

Related Questions