Darkranger85 Posted January 24, 2012 Share Posted January 24, 2012 Below and 2 snips of code that do exactly the same thing, the only difference is that one uses a constant and the other a variable. <?php //Classes// class Circle { const pi = 3.141; public function Area($radius){ return self::pi * ($radius*$radius); } } //Program Variables// $circle = new Circle; //Program Code// echo $circle->Area(100); ?> <?php //Classes// class Circle { public $pi = 3.141; public function Area($radius){ return $this->pi * ($radius*$radius); } } //Program Variables// $circle = new Circle; //Program Code// echo $circle->Area(100); ?> Please note that I am a novice and so I'm sure I'm completely wrong about everything I am going to say. I'm sure there is a purpose, I just can't figure it out. As far as I can see, the only thing the "const" does is make it so there more things for me to have to remember, because I have to write it differently and I have to use "self::" rather than "$this->" So, to my main question. Why would I use a const? I could put a constant value inside a variable and as long as I don't change it it's going to say "constant." So why would I wan't to confuse myself with a bunch of other codes? Thank you all! Looking forward to being wrong lol! Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/ Share on other sites More sharing options...
Philip Posted January 24, 2012 Share Posted January 24, 2012 I could put a constant value inside a variable and as long as I don't change it it's going to say "constant." So why would I wan't to confuse myself with a bunch of other codes? To a) ensure that the data is indeed a constant (e.g. you will get an warning/error if you try to overwrite the value) and b) for whenever you deal with other programmers so they can easily tell that it is a constant Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310532 Share on other sites More sharing options...
Darkranger85 Posted January 24, 2012 Author Share Posted January 24, 2012 Ahhh, I see. So basically, functionally speaking, it makes no difference at all? It's just for future reference and safety? Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310544 Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 Ahhh, I see. So basically, functionally speaking, it makes no difference at all? It's just for future reference and safety? Is the value of Pi ever going to change? Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310553 Share on other sites More sharing options...
Darkranger85 Posted January 24, 2012 Author Share Posted January 24, 2012 Is the value of Pi ever going to change? No, but that was not my point. My point was that I can put a constant inside a "variable" and it's still technically a constant as long as I don't change it. I basically thought that the only difference between a const and a var was that I had to learn new code for using them. But, even though theoretically there is no difference, I guess it's to ensure you don't accidentally change your constant and so that others know it's supposed to be a constant. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310557 Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 You don't have to learn new code. Constants are essentially the same thing as variables, yes, except constants cannot change. They are just used in different contexts, and that's all you need to know; i. e., when to use what where for whatever reason. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310559 Share on other sites More sharing options...
Darkranger85 Posted January 24, 2012 Author Share Posted January 24, 2012 You do have to learn new code. "Const" rather than just declaring the variable name, not that this is difficult. And the use of "self::" rather than "$this->" That may not seem like a lot to an experienced programmer, but to someone that is for the first time getting into classes and such, it does lead to some confusion. Especially when I didn't really see the functional point in using said codes when variables, in theory, do the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310561 Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 If you want to learn classes, I suggest learning C++. I find that classes in C++ make a lot more sense than in PHP (such as the "self" keyword). From there, it's an easy transition into PHP. And just to further your quest in education, and to emphasize my suggestion, check this page out: https://bugs.php.net/bug.php?id=30934 Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310562 Share on other sites More sharing options...
trq Posted January 24, 2012 Share Posted January 24, 2012 If you want to learn classes, I suggest learning C++. I find that classes in C++ make a lot more sense than in PHP (such as the "self" keyword). From there, it's an easy transition into PHP. Learning another language just to learn about classes is ridiculous. There is nothing particularly odd about PHP's OOP implementation. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310565 Share on other sites More sharing options...
Darkranger85 Posted January 24, 2012 Author Share Posted January 24, 2012 If you want to learn classes, I suggest learning C++. I find that classes in C++ make a lot more sense than in PHP (such as the "self" keyword). From there, it's an easy transition into PHP. And just to further your quest in education, and to emphasize my suggestion, check this page out: https://bugs.php.net/bug.php?id=30934 I'm not just learning about classes. I'm learning PHP in general because I would like to become a web programmer eventually. Learning C++ just to learn about classes seems a bit counter productive to me. Not that I don't have an interest in eventually dabbling in C++, but PHP and probably Javascript are my first ones to tackle I think, but I do thank you for your suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310675 Share on other sites More sharing options...
Philip Posted January 24, 2012 Share Posted January 24, 2012 You do have to learn new code. [...] That may not seem like a lot to an experienced programmer, but to someone that is for the first time getting into classes and such, it does lead to some confusion. Just wait until you get into static methods/properties. You'll need to learn pretty much the same syntax Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310723 Share on other sites More sharing options...
scootstah Posted January 24, 2012 Share Posted January 24, 2012 I don't see it mentioned here so I will. Constants also ignore scope and are globally available anywhere in your script. $foo = 'bar'; function blah() { echo $foo; } This does not work. But this does: define('FOO', 'bar'); function blah() { echo FOO; } Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310730 Share on other sites More sharing options...
trq Posted January 24, 2012 Share Posted January 24, 2012 I don't see it mentioned here so I will. Constants also ignore scope and are globally available anywhere in your script. $foo = 'bar' ; Except that in this case we are talking about class constants. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310767 Share on other sites More sharing options...
DavidAM Posted January 24, 2012 Share Posted January 24, 2012 I am not an OOP expert. But one point seems to have been missed in this conversation. A Class constant, as in your original example, is a member of the CLASS and is NOT a member of the OBJECT. This is the reason for the different way that you reference it. Your second code sample would more closely resemble your first if it were: <?php //Classes// class Circle { static $pi = 3.141; public function Area($radius){ return self::$pi * ($radius*$radius); } } //Program Variables// $circle = new Circle; //Program Code// echo $circle->Area(100); ?> The static keyword makes the $pi property a class property. It belongs to the class and not to the object. If the variable's value were changed by a program, it would change for ALL of the objects instantiated from the class. Since it is a member of the class it is referenced using the self:: construct (or using Circle::$pi, in this case). So, the answer to your underlying question as to why use $this-> vs. using self:: is not because of the constant. It is because of the scope or ownership of the element being referenced. Perhaps one of the OOP guys can explain it better. And, by the way, there is no way (in PHP) to create an "Object Constant". Constants (in a class) always are static class constants. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310771 Share on other sites More sharing options...
Proletarian Posted January 24, 2012 Share Posted January 24, 2012 I'm not just learning about classes. I'm learning PHP in general because I would like to become a web programmer eventually. Learning C++ just to learn about classes seems a bit counter productive to me. Not that I don't have an interest in eventually dabbling in C++, but PHP and probably Javascript are my first ones to tackle I think, but I do thank you for your suggestions. It's funny cuz I'm coming at it from the opposite point as you. I learned C++ first and am now learning PHP. But you are right, learning a different language just to learn the concept of object oriented programming is counter-productive. I just have a bias towards my perspective. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310810 Share on other sites More sharing options...
Darkranger85 Posted January 24, 2012 Author Share Posted January 24, 2012 lol my head is starting to hurt. I'm hoping that I get the hang of these things as I need them. In my experience actually making use of a code or whatnot in an actual project always makes it stick in your head better than arbitrary examples lol. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310818 Share on other sites More sharing options...
scootstah Posted January 25, 2012 Share Posted January 25, 2012 I don't see it mentioned here so I will. Constants also ignore scope and are globally available anywhere in your script. $foo = 'bar' ; Except that in this case we are talking about class constants. Oh, haha. That's what I get for skimming. Quote Link to comment https://forums.phpfreaks.com/topic/255646-why-use-constants-as-opposed-to-variables/#findComment-1310873 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.