Jump to content

Why use constants as opposed to variables?


Darkranger85

Recommended Posts

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!

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :o

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.