Ryan
Ryan

Reputation: 6045

Does instantiating multiple mysqli objects in a single PHP script get optimized?

I have a base model class that wraps mysqli. When I instantiate multiple models each one calls new mysqli(). If the parameters passed to mysqli() are the same will it be optimized for me and only use one connection?

I guess another way to ask this is...is this:

$mysqli1 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli2 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli3 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

more or less equivalent to this:

$mysqli1 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli2 = $mysqli1;
$mysqli3 = $mysqli1;

Edit: I remember reading somewhere that it does return a copy if you're already connected but I can't find it now.

Upvotes: 0

Views: 254

Answers (1)

Dharman
Dharman

Reputation: 33238

No, it will open a new connection to the database. Every time you create an instance of mysqli class, even if with the same parameters, it will create a new instance.

Upvotes: 1

Related Questions