Skip to content

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

Description

@ckrudelux

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'));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions