Arnsenal Posted July 22, 2012 Share Posted July 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/266096-bypassing-passing-mysqli-object-into-functions/ Share on other sites More sharing options...
requinix Posted July 22, 2012 Share Posted July 22, 2012 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(); Quote Link to comment https://forums.phpfreaks.com/topic/266096-bypassing-passing-mysqli-object-into-functions/#findComment-1363576 Share on other sites More sharing options...
Arnsenal Posted July 22, 2012 Author Share Posted July 22, 2012 Alright!! Thanks Much Requinix, That's what I had in mind but didn't know how to use static properties to do such a thing. That will work perfectly thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/266096-bypassing-passing-mysqli-object-into-functions/#findComment-1363588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.