unemployment Posted July 6, 2011 Share Posted July 6, 2011 I'm trying to learn OOP. I mean I just started today and I don't understand why this doesn't echo anything. Please help. class user_info { public $first_name = 'Jason'; public function info_data(){ global $first_name; echo $first_name; } } $data = new user_info(); $data->info_data(); Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2011 Share Posted July 6, 2011 Your first step is to forget that you ever saw the php global keyword, in procedural code or OOP. Don't use global. That's not how you reference a class property/variable inside a class method. You use the $this->class_property syntax. Here's the first example from the php.net OOP documentation - <?php class SimpleClass { // property declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239062 Share on other sites More sharing options...
unemployment Posted July 6, 2011 Author Share Posted July 6, 2011 Your first step is to forget that you ever saw the php global keyword, in procedural code or OOP. Don't use global. That's not how you reference a class property/variable inside a class method. You use the $this->class_property syntax. Here's the first example from the php.net OOP documentation - <?php class SimpleClass { // property declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; } } ?> Thanks. Ok that makes sense, but why shouldn't I use global? Global is used a lot in the php documentation from what I can tell. Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239063 Share on other sites More sharing options...
AyKay47 Posted July 6, 2011 Share Posted July 6, 2011 global is used to create a reference in local scope to a variable that is in the global scope...classes work differently, they are both in the classes scope Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239066 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 Globals are bad If we're both working on a PHP project, and you take a variable defined in the global scope, and modify that value in the local scope of a function without any reference to it, I get very mad. I expect that variable to have a certain value, and some function changes it. The function call doesn't have any reference to it. I now have to comment out each line until I find out WHERE that value has been changed, or GREP through my entire project searching for 'global [^;]*?\$varname' Heck, if someone was really mean, they'd do something like <?php $var = 'somevalue'; function pissyouoff() { $hidden = 'var'; $GLOBALS[$hidden] = strrev($GLOBALS[$hidden]); } pissyouoff(); echo $var; ?> Now imagine pissyouoff() was included from another file, and there was about 20-30 more user-defined functions called on that page Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239069 Share on other sites More sharing options...
unemployment Posted July 6, 2011 Author Share Posted July 6, 2011 Yes, I understand. Good to know. Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239073 Share on other sites More sharing options...
AyKay47 Posted July 6, 2011 Share Posted July 6, 2011 Globals are bad If we're both working on a PHP project, and you take a variable defined in the global scope, and modify that value in the local scope of a function without any reference to it, I get very mad. I expect that variable to have a certain value, and some function changes it. The function call doesn't have any reference to it. I now have to comment out each line until I find out WHERE that value has been changed, or GREP through my entire project searching for 'global [^;]*?\$varname' Heck, if someone was really mean, they'd do something like <?php $var = 'somevalue'; function pissyouoff() { $hidden = 'var'; $GLOBALS[$hidden] = strrev($GLOBALS[$hidden]); } pissyouoff(); echo $var; ?> Now imagine pissyouoff() was included from another file, and there was about 20-30 more user-defined functions called on that page that was a very interesting way of describing it.... Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239075 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 Would you prefer if I used words like namespace pollution, non-locality or implicit coupling? I was trying to put it into simple terms with an example Yes, globals can be used for quick one-offs, and singleton classes are pretty much globals confined to a class, but you should only use them when you completely understand why you shouldn't use them. Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239077 Share on other sites More sharing options...
AyKay47 Posted July 6, 2011 Share Posted July 6, 2011 Would you prefer if I used words like namespace pollution, non-locality or implicit coupling? I was trying to put it into simple terms with an example Yes, globals can be used for quick one-offs, and singleton classes are pretty much globals confined to a class, but you should only use them when you completely understand why you shouldn't use them. that was a very defensive response to a playful post....geeze man lighten up.. and namespace polution would not make much sense to use here now would it.. Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239090 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 Eyes rolling = sarcasm. Didn't seem playful to me. Besides, I thought my last statement was pretty playful Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239097 Share on other sites More sharing options...
ignace Posted July 6, 2011 Share Posted July 6, 2011 and namespace polution would not make much sense to use here now would it.. Yes, it does. The global namespace is implied. Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239264 Share on other sites More sharing options...
KevinM1 Posted July 6, 2011 Share Posted July 6, 2011 Thanks. Ok that makes sense, but why shouldn't I use global? Global is used a lot in the php documentation from what I can tell. No, it's not. Anyway, as to why 'global' is bad.... Functions should be seen as black boxes. Their innards (function definition) should be hidden from view. All that one needs to know about a function in order to properly use it is its signature - its name, explicitly required parameters, and return type (which is kind of glossed over in PHP). 'Global' adds implicitly required parameters to the mix, thereby muddying up the function's signature. Further, it breaks the whole idea of how a function should work, as it creates a link in the function to the environment it will be used in. Rather than being abstract black boxes which can be used in a variety of situations, they're now bound to a certain kind of context. The problem only magnifies as the use of 'global' continues. Code is dependent on other code with no real indication of why. A change in one part of the system would likely necessitate a change in another. This is known as coupling, and is considered very bad. And, if the required 'global' is missing, or not of the proper type, it can be very difficult to find and fix, requiring one to actually dive deep into the code rather than simply look at the function signatures to get a feel for what's going on. Besides, 'global' is completely unnecessary anyway. There's already a mechanism to pass variables into a function - the argument list. Use it, love it, and if you feel you're passing too many parameters into a function, chances are your design could be improved. Quote Link to comment https://forums.phpfreaks.com/topic/241218-basic-oop/#findComment-1239268 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.