PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] PHP defining namespaces on composer's vendor packages

 January 30, 2022     composer-php, namespaces, php     No comments   

Issue

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?


Solution

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.



Answered By - deceze
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing