Page 1 of 2

Why use constants?

Posted: Wed Nov 09, 2005 2:43 pm
by Luke
What is the point of constants? What advantages to they give me? Why would I use them?

Posted: Wed Nov 09, 2005 2:55 pm
by foobar
Constants are used, as the name itself suggests, because you can't change them later on. They are, however, only valid for each single page execution. However, regular variables tend to be a bit faster for some reason, probably due to the additional parsing required for constants.

Posted: Wed Nov 09, 2005 2:57 pm
by Luke
Anybody have any examples of why anybody would need them?

Posted: Wed Nov 09, 2005 3:10 pm
by foobar
One armed space goat wrote:Anybody have any examples of why anybody would need them?
Things like configuration/language constants are popular examples of this. Basically, stuff you don't want anybody/anything to change during page execution, just to make sure you or someone else doesn't screw it up by accident.

Posted: Wed Nov 09, 2005 3:15 pm
by Luke
Oh ok... thanks :D

Posted: Wed Nov 09, 2005 3:17 pm
by foobar
No problem. I'm thinking this should've been done on some IM, since we're both online, but whatever... :P

Posted: Wed Nov 09, 2005 3:18 pm
by Luke
HA.. yea, but somebody else may have read this and been helped as well.

Posted: Wed Nov 09, 2005 3:20 pm
by Luke
So, can you define constants any other way than define()?

Posted: Wed Nov 09, 2005 3:26 pm
by foobar
One armed space goat wrote:So, can you define constants any other way than define()?
The [url=http://php.net/language.constants]PHP Manual[/url] wrote: You can define a constant by using the define()-function.
Nothing else is said.

Posted: Wed Nov 09, 2005 3:27 pm
by Luke
I read that part... that's how I know you can do it that way... is there any other way to do it? ie:

Code: Select all

CONSTANT = "something";

Posted: Wed Nov 09, 2005 3:31 pm
by foobar
One armed space goat wrote:I read that part... that's how I know you can do it that way... is there any other way to do it? ie:

Code: Select all

CONSTANT = "something";
Try. It.

Posted: Wed Nov 09, 2005 3:43 pm
by Chris Corbyn
One armed space goat wrote:I read that part... that's how I know you can do it that way... is there any other way to do it? ie:

Code: Select all

CONSTANT = "something";
Depends if we're talking OOP or procedural ;) In PHP5 at least:

Code: Select all

class foo
{

    const color = 'red';    //Accessed by    self::color
    const flavor = 'tomato';   // self::flavor
    private $private_property;  // $this->private_property (read/write only inside this class)
    public $public_property = 'default_value';  // $this->public_property  (read/write anywhere)

    public function dump()
    {
        print_r($this);
    }

}

Posted: Wed Nov 09, 2005 3:45 pm
by Jeroen Oosterlaar
I think it is just a matter of definition. When I write a configuration file, it feels logical to define constants as... constants, such as the database host address, username, password etc. I could just as well define such properties as variables, because I know that they will not be changed by other routines, but with respect to the beauty of the design, using constants is preferable.

Posted: Wed Nov 09, 2005 3:45 pm
by Jenk
Constants are also handy because they are similar to superglobals in that they can be accessed anywhere, by any scope.

e.g.

Code: Select all

<?php

define('MYCONST', 10);
//within same scope..
echo MYCONST;

function myFunc() 
{
    //within a function..
    echo MYCONST;
}

class myClass
{
    //within a class
    var $myVar = MYCONST;
    function myFunc() 
    {
        //within a method..
        echo MYCONST;
    }
}
?>

Posted: Wed Nov 09, 2005 3:49 pm
by Chris Corbyn
Put simply....

You use constants where the value never needs to change
Use variables where the data can change (i.e. variable ;) )

Coding in other languages perhaps makes the distinction clearer :)