Jump to content

Bypassing Passing MYSQLI object into functions


Arnsenal

Recommended Posts

Hello,

 

I have a db.php that initializes a MySQLI object.

 

I have other scripts that include this db.php script.

 

Is it possible to use the MYSQLI object inside functions from these other scripts that are including the db.php without passing the MYSQLI object as a parameter?

 

Basically is there a way I can make the scope inside function have access to mysqli object without passing it in as a parameter?

 

Thanks

What you're thinking of is global variables and they are almost always the wrong thing to use.

 

Better would be to use a class to get the object, like

class DB {

private static $mysqli = null;

public static function get() {
	if (!self::$mysqli) {
		self::$mysqli = // new mysqli
	}
	return self::$mysqli;
}

}

$db = DB::get();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.