thephpleague / container

Small but powerful dependency injection container

Home Page:http://container.thephpleague.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Preceeding slash confusing fully qualified class names

M1ke opened this issue · comments

This may be a known limitation, or even designed in, but we've stumbled across an interesting case of class naming as an issue:

// We might be using a string to run logic around loops/interpolation
$some_class = "\\Namespace\\ClassName";
$container->add($some_class);

Elsewhere

$container->get(ClassName::class)

This later access will not find the existing entry due to the leading slashes.

I wonder if there's a case for stripping leading slashes during name resolution - I can't see a situation where somebody would want "$key" and "\\$key" to resolve differently, and it avoids this being a hard-to-spot bug in a larger container setup.

Happy to PR for the feature if acceptable.

Hi @M1ke

Yeah I'd be in favour of this, would need to normalise on strings used to add to the container as well as retrieving from the container so as to ensure it's not a breaking change for anyone who's taken account of this in their application code.

I'm not certain but there might be a gotcha where someone adds a class name string as alias and concrete while adding so will be worth having a test for that situation.