Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ckrudelux opened this issue Mar 7, 2023 · 0 comments

Comments

@ckrudelux
Copy link

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'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant