Skip to content

How to get a named instance from the container? #199

Description

@adjenks

I created a named instance like so:

$rules = [
	'$logger2' => [
		Dice::INSTANCE => function () use ($config) {
			//do stuff;
			return $thing;
		}
	]
];
$dice = $dice->addRules($rules);

And I tried to get it like so:

$logger2 = $dice->create('$logger2');

But it did not work because create() only expects class names. It throws:

PHP Fatal error: Uncaught ReflectionException: Class $logger2 does not exist in //vendor/level-2/dice/Dice.php:107

I understand I can make it a dependency of a class and then create the class, but what if I just want to access it as is?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions