Level-2 / Dice

Dice - a lightweight Dependency Injection Container for PHP

Home Page:https://r.je/dice

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Docs: Section 3.1 3. Named instances: Must use Dice::INSTANCE as key, not 'instance'

someone635 opened this issue · comments

$dice->addRules([

    //Define a named instance called $MyDirectoryIterator

    //and configure the instance constructor arguments



    '$MyDirectoryIterator' => [

        //An instance of the DirectoryIterator class will be created

        'instanceOf' => 'DirectoryIterator',

        //When the DirectoryIterator is created, it will be passed the string '/tmp' as the constructor argument

        'constructParams' => ['/tmp']

    ],



    //Define a rule to use the named instance as a substitution

    //when creating A instances



    'A' => [

        'substitutions' =>

            [

                'Iterator' => ['instance' => '$MyDirectoryIterator']

            ]

        ]

    ]

);





//Now, when $a is created, it will be passed the Iterator configured as $MyDirectoryIterator



$a = $dice->create('A');

In this example given in the docs, it says we can use 'instance' as a key for named instance substitution, but that does not work. Only using Dice::INSTANCE as a key works. (using latest dev-master package from composer)

Thanks, yes you're right, I've updated the docs. It should be Dice::INSTANCE.