php_guest Posted March 12, 2011 Share Posted March 12, 2011 I use $db object to handle database function like $db->get_row for example. Is there any way that I don't need to put into every function global $db or do I actually need to repeat in every function global $db? function example(){ global $db //Do I need to repeat this in each function? } Quote Link to comment https://forums.phpfreaks.com/topic/230408-global-variables/ Share on other sites More sharing options...
witnot1 Posted March 12, 2011 Share Posted March 12, 2011 Hi , I would recommend using a framework i.e. a mvc framework like Zend which uses object orientation. class DB_Object { protected $_db; /* Getters and Setters*/ public function setDb($value) { $this->_db = $value; } } Then you can reference the member variable. Quote Link to comment https://forums.phpfreaks.com/topic/230408-global-variables/#findComment-1186560 Share on other sites More sharing options...
php_guest Posted March 12, 2011 Author Share Posted March 12, 2011 but without class, the only way to access $db object inside function is to use global $db every time in each function, right? Quote Link to comment https://forums.phpfreaks.com/topic/230408-global-variables/#findComment-1186568 Share on other sites More sharing options...
witnot1 Posted March 12, 2011 Share Posted March 12, 2011 in your case yes, but without a framework, I would at least have a static class where I can reference my db object like so class Database { // Store the single instance of Database private static $m_pInstance; private function __construct() { ... } public static function getInstance() { if (!self::$m_pInstance) { self::$m_pInstance = new Database(); } return self::$m_pInstance; } } Then use that static object as such $pDatabase = Database::getInstance(); $aResult = $pDatabase->query('...'); globals are not nice to work with. Witnot Quote Link to comment https://forums.phpfreaks.com/topic/230408-global-variables/#findComment-1186571 Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2011 Share Posted March 12, 2011 You pass the $db object into the function as a parameter when you call the function, which is what the $value parameter is in the code witnot1 posted. It does not matter if you are using a function or a class method. The reason you pass it in as a parameter, instead of using the global keyword, is so that your function or class remains general purpose and is not dependent on the specific name of a variable in the calling code. Suppose that you need to have two different database connections at the same time in an application. If you use the global keyword, you would need to use more code to shuffle the two different connections back and forth between the $db variable before each call of your function or you would need to duplicate your function and modify the code in one copy to use the different db object name. However, why would you go through the extra work and coding to do that? Functions and classes are supposed to make writing code easier, not create more work. By passing the db object (or any other main program variable) into the function when you call it, you can use any number of different db objects in your code simply by putting the correct one in as a parameter when you call your function. You can write and test your function once and then use it with one db object or 10 different db objects with the minimum lines of code or extra work keeping track of what you are doing at the current time in your program. Quote Link to comment https://forums.phpfreaks.com/topic/230408-global-variables/#findComment-1186572 Share on other sites More sharing options...
witnot1 Posted March 12, 2011 Share Posted March 12, 2011 Rather use a MVC Framework where you can have config file with several db connections. MVC will save you guys a lot of problems Witnot Quote Link to comment https://forums.phpfreaks.com/topic/230408-global-variables/#findComment-1186573 Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2011 Share Posted March 12, 2011 Using a static class has the same disadvantages as using the global keyword, your code is dependent on the specific name you have hard coded into the function/class that is using it. Extrapolate this problem to a different example of a user object ($user instead of a $db object), where you might have an administrator logged into a site and you want to create a page that lets him access and edit a specific user account information. By keeping your functions and classes general purpose, doing this would be simple. The page would check if the current visitor (the admin) is logged in by putting the $user variable into the function calls and then simply create a second instance of the user class (in $edit_user for example) for the visitor he wants to access. You would simply reuse and call your existing functions/classes with the correct instance of the $user or $edit_user that you need at the time. Quote Link to comment https://forums.phpfreaks.com/topic/230408-global-variables/#findComment-1186574 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.