Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

get and has order is important #51

Closed
wants to merge 1 commit into from

Conversation

svycka
Copy link
Contributor

@svycka svycka commented Nov 6, 2015

if has() returns false and works as expected then how can I check if manager can give me and an instance

@svycka
Copy link
Contributor Author

svycka commented Nov 6, 2015

more on this #50

@weierophinney
Copy link
Member

I'm sorry, but this is by design.

has() only checks the following sources:

  • services already registered or created
  • known invokable mappings
  • known factory mappings
  • all abstract factories

The "auto-add invokable classes" flag is specific to the plugin manager, and the assumption is that any fully qualified class name can be passed to the plugin manager, and it will attempt to create an instance. However, this is not the same as having an instance already; it's an opportunistic approach that could fail (e.g., if the constructor has required arguments!).

As such, having has() report true for such instances can and would be misleading — and far moreso than having it report false.

If you want equivalent behavior, but also want has() to report true, you could do the following:

  • create an abstract factory, WHERE
  • canCreateServiceWithName() returns true when class_exists($requestedName); THEN
  • change the $autoAddInvokableClass property to false, AND
  • register your abstract factory with your plugin manager.

This will have the effect of having has() return true if the class exists.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants