Why use constants?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Why use constants?

Post by Luke »

What is the point of constants? What advantages to they give me? Why would I use them?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Anybody have any examples of why anybody would need them?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Oh ok... thanks :D
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

No problem. I'm thinking this should've been done on some IM, since we're both online, but whatever... :P
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

HA.. yea, but somebody else may have read this and been helped as well.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

So, can you define constants any other way than define()?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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";
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

}
Jeroen Oosterlaar
Forum Commoner
Posts: 37
Joined: Sun Nov 06, 2005 4:12 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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;
    }
}
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
Post Reply