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

PDO or Mysqli, named instances and shared instances error Undefined Array Key

ckrudelux opened this issue · comments

Loading PDO or Mysqli with two different and shared instances doesn't work. I've been expermenting some and this is what I have come up with.
PDO and Mysqli behaves the same.

This does not work:

$dice = new \Dice\Dice;
    $dice = $dice->addRules([
      'PDO' => [
        'shared' => true,
        'constructParams' => [
          'mysql:host=127.0.0.1;dbname=test1',
          'root',
          ''
        ]
      ],
      '$test' => [
        'shared' => true,
        'constructParams' => [
          'mysql:host=127.0.0.1;dbname=test2',
          'root',
          ''
        ],
        'instanceOf' => 'PDO'
      ]
    ]);

    echo get_class($dice->create('$test'));

I get this error

Undefined array key "$test"
Trace:
#0 \htdocs\Dice.php(134): {closure}(2, 'Undefined array...', '\htdocs\...', 134)
#1 \htdocs\Dice.php(96): Dice\Dice->Dice{closure}(Array, Array)
#2 \htdocs\index.php(74): Dice\Dice->create('$test')
#3 {main}

But if change shared to false on the named instance ($test) it works. Doing false on PDO and true on $test doesn't work.

$dice = new \Dice\Dice;
    $dice = $dice->addRules([
      'PDO' => [
        'shared' => true,
        'constructParams' => [
          'mysql:host=127.0.0.1;dbname=test1',
          'root',
          ''
        ]
      ],
      '$test' => [
        'shared' => false,
        'constructParams' => [
          'mysql:host=127.0.0.1;dbname=test2',
          'root',
          ''
        ],
        'instanceOf' => 'PDO'
      ]
    ]);

    echo get_class($dice->create('$test'));

If I make a empty dummy class or if I extend PDO with an empty class it works

class tmpPDO {
      public function __construct($a,$b,$c){}
    }


    $dice = new \Dice\Dice;
    $dice = $dice->addRules([
      'tmpPDO' => [
        'shared' => true,
        'constructParams' => [
          'mysql:host=127.0.0.1;dbname=test1',
          'root',
          ''
        ]
      ],
      '$test' => [
        'shared' => true,
        'constructParams' => [
          'mysql:host=127.0.0.1;dbname=test2',
          'root',
          ''
        ],
        'instanceOf' => 'tmpPDO'
      ]
    ]);

    echo get_class($dice->create('$test'));

Extends PDO works:

class tmpPDO extends PDO {
}


    $dice = new \Dice\Dice;
    $dice = $dice->addRules([
      'tmpPDO' => [
        'shared' => true,
        'constructParams' => [
          'mysql:host=127.0.0.1;dbname=test1',
          'root',
          ''
        ]
      ],
      '$test' => [
        'shared' => true,
        'constructParams' => [
          'mysql:host=127.0.0.1;dbname=test2',
          'root',
          ''
        ],
        'instanceOf' => 'tmpPDO'
      ]
    ]);

    echo get_class($dice->create('$test'));