jaymc Posted June 23, 2009 Share Posted June 23, 2009 I have extended the mysqli class like below $my = new db_class("192.168.1.2", "root", "password", "db"); Im new to classes so heres my question In order to use the $my to gain mysql functionality within another function outside of the class, do I have to call global $my at the start of the function like so function cheese() { global $my; $q = $my->query("select * from users); } I am kind of hoping I can use $my where ever and when ever I wish as long as the mysq.class.php file is included, without having to use global or $GLOBALS etc Is the way I have shown above the correct method? Quote Link to comment https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/ Share on other sites More sharing options...
Ken2k7 Posted June 23, 2009 Share Posted June 23, 2009 You can extend on this, but try something like this - <?php class db_class { private static $connection = null; public static function singleton ($host = '', $user = '', $password = '', $db = '') { if (is_null($connection)) self::__construct($host, $user, $password, $db); return self::$connection; } } $my = db_class::singleton("192.168.1.2", "root", "password", "db"); Then use it like - <?php include 'db_class.inc'; // or .php, whatever you use. function cheese () { $my = db_class::singleton(); // more code } Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/#findComment-861635 Share on other sites More sharing options...
jaymc Posted June 23, 2009 Author Share Posted June 23, 2009 I usually define $my once at the top of the script that way I can use it inside of any function. The example you have given above still requires $my to be defined within the function requiring it I need something like this $my = new db_class("192.168.1.2", "root", "password", "db"); function cheese() { $q = $my->query("select * from users); } function beans() { $q = $my->query("select * from users); } function sausage() { $q = $my->query("select * from users); } Hence no mention of $my in any function in order to use it.. Quote Link to comment https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/#findComment-861637 Share on other sites More sharing options...
Philip Posted June 23, 2009 Share Posted June 23, 2009 Globals is not the best way to go about this, but instead you should pass the variable by argument into the function: function fooBar($connection) { $result = $connection->query("SELECT `name` FROM `users`"); // ... more stuff here } $my = new db_class("192.168.1.2", "root", "password", "db"); fooBar($my); Quote Link to comment https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/#findComment-861639 Share on other sites More sharing options...
jaymc Posted June 23, 2009 Author Share Posted June 23, 2009 Ha that still requires to call $my in some kind of additional way whether its using global, the example above or as a paramater I guess there is no way to be able to access $my inside a function without have some kind of reference inside the function or passed in as a param? Quote Link to comment https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/#findComment-861640 Share on other sites More sharing options...
Philip Posted June 23, 2009 Share Posted June 23, 2009 Like Ken said, you could use a static method, but it wouldn't be as portable as using global or as a parameter. (mainly because if you change your class name, the method, etc, you'd have to go into each function and change that rather than just one variable) Quote Link to comment https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/#findComment-861641 Share on other sites More sharing options...
jaymc Posted June 23, 2009 Author Share Posted June 23, 2009 Ok cool I will just use global Thanks for input. Quote Link to comment https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/#findComment-861644 Share on other sites More sharing options...
Ken2k7 Posted June 23, 2009 Share Posted June 23, 2009 Like Ken said, you could use a static method, but it wouldn't be as portable as using global or as a parameter. (mainly because if you change your class name, the method, etc, you'd have to go into each function and change that rather than just one variable) Something like that would belong in Framework and usually class names don't get changed too often. If you want to change the method name, just have the old one to call the new one. =\ Quote Link to comment https://forums.phpfreaks.com/topic/163309-solved-extended-mysqli-class-function/#findComment-861659 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.