dennismonsewicz Posted January 13, 2009 Share Posted January 13, 2009 In working with OOP I am seeing a lot of stuff like this global $var what does the global do exactly? Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/ Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 global pulls in the variable from the main part of the script: http://us3.php.net/global edit: IMO, using global in PHP is bad programming. there are other better ways to access variables Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736370 Share on other sites More sharing options...
dennismonsewicz Posted January 13, 2009 Author Share Posted January 13, 2009 ah gotcha... what ways would those be? Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736379 Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 depends on what you are trying to do. most of the time, you can just use function arguments though Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736390 Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 ah gotcha... what ways would those be? I setup my DB Class with functions, so it can be called from anywhere in my script without adding a ton of overhead. In these functions I make the instantiated db class global so I do not have to have that object to pass in each time I call it. That is a good way to use it, more for versatility. If you just have a regular variable say you want to add x + y, I would just pass both those in as parameters, as that is what the function is designed to do. So it all depends on the circumstance. Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736392 Share on other sites More sharing options...
dennismonsewicz Posted January 13, 2009 Author Share Posted January 13, 2009 cool thanks! I am trying to practice more OOP... still kind of new to OOP... not to sure i have a full grasp of it Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736395 Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 cool thanks! I am trying to practice more OOP... still kind of new to OOP... not to sure i have a full grasp of it If you want more in depth explanation post the code where you are using global at. Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736397 Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 for that DB case, i usually use the Singleton Design Pattern Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736398 Share on other sites More sharing options...
dennismonsewicz Posted January 13, 2009 Author Share Posted January 13, 2009 cool thanks! I am trying to practice more OOP... still kind of new to OOP... not to sure i have a full grasp of it If you want more in depth explanation post the code where you are using global at. Well I am working within Joomla (CMS) to build a custom component and within its backend they have a pre-built function that will do pagination for you and they set a global.. so I am using the code but not necessarily understanding why its being used... global $mainframe; $context = 'com_component.' . $d . '.list.'; $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', 20, 'int' ); $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' ); Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736413 Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 the $mainframe variable in Joomla is it's core. you can use it to access core information. since they support PHP4, global is the only way to share that variable around. in this case, you have to use global if you want to use the mainframe. Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736416 Share on other sites More sharing options...
dennismonsewicz Posted January 13, 2009 Author Share Posted January 13, 2009 ah gotcha... cool makes sense now. I am just so used to building my own customized CMS's that i am not used to having to use global vars. Thanks for all of the help! Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736418 Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 for that DB case, i usually use the Singleton Design Pattern I will have to read up on that. cool thanks! I am trying to practice more OOP... still kind of new to OOP... not to sure i have a full grasp of it If you want more in depth explanation post the code where you are using global at. Well I am working within Joomla (CMS) to build a custom component and within its backend they have a pre-built function that will do pagination for you and they set a global.. so I am using the code but not necessarily understanding why its being used... global $mainframe; $context = 'com_component.' . $d . '.list.'; $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', 20, 'int' ); $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' ); Just remember, because one script does it that way does not mean it is the correct way. Especially a script like Joomla where their ultimate goal is being able to use the script on any version of php etc. Not really a good script to learn OOP from imo. Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736419 Share on other sites More sharing options...
rhodesa Posted January 13, 2009 Share Posted January 13, 2009 I will have to read up on that. in a nutshell: <?php class db { private static $instance; private $cnx; static function getInstance(){ return self::$instance; } public function __construct ( $host, $user, $pass, $db ) { //Check for singleton if(self::$instance) throw new Exception("An instance already exists"); //Connect $this->cnx = mysql_connect($host,$user,$pass); mysql_select_db($db,$this->cnx); //Add singleton self::$instance = $this; } public function query ( $sql ) { } } //Connect $db1 = new db('host','user','pass','db'); //you can now use the variable $db1 as normal $db1->query(); //or get it via the static method $db2 = db::getInstance(); //or in a function function do_query ( ) { $db = db::getInstance(); } ?> be careful though, people tend to go crazy with singletons and use more memory then they need to Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736421 Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 I will have to read up on that. in a nutshell: <?php class db { private static $instance; private $cnx; static function getInstance(){ return self::$instance; } public function __construct ( $host, $user, $pass, $db ) { //Check for singleton if(self::$instance) throw new Exception("An instance already exists"); //Connect $this->cnx = mysql_connect($host,$user,$pass); mysql_select_db($db,$this->cnx); //Add singleton self::$instance = $this; } public function query ( $sql ) { } } //Connect $db1 = new db('host','user','pass','db'); //you can now use the variable $db1 as normal $db1->query(); //or get it via the static method $db2 = db::getInstance(); //or in a function function do_query ( ) { $db = db::getInstance(); } ?> be careful though, people tend to go crazy with singletons and use more memory then they need to Yea that is exactly what I would need. Be a bit easier than doing that global stuff. Thanks for the example. And yea I only see myself using singleton for the db connection. Thanks rhodesa. Quote Link to comment https://forums.phpfreaks.com/topic/140695-solved-global-question/#findComment-736422 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.