1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What Is The USe For Constants?

Discussion in 'PHP' started by gobbly2100, Jan 6, 2009.

  1. #1
    Hey,

    I am learning PHP and was just reading up about constants but I don't see how they are actually any use, what would be a use for constants in PHP?
     
    gobbly2100, Jan 6, 2009 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You use constants for values that you don't expect to change througout the program. For example, database details (when you only deal with one database), number of hours in a day, etc.

    For the database details example, the main advantage of using constants is that when in the future any of the details changes, you'll only need to update the value of this constants without having to go through all your code to search and replace each occurance of it.

    For the number of hours in a day example, it will never change, but it's still a good idea to make it as a constant anyway because with constants you can name it something that clearly identifies what the number represents (e.g.: $NUM_OF_HOURS_PER_DAY). So when you look at the code you know instantly the value you are looking at is the number of hours per day. If you look at the number '24', it could be anything. And especially when your code gets bigger and you put more and more hardcoded values in, your code will get less and less readable.
     
    phper, Jan 6, 2009 IP
  3. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Pretty sure by constants they mean something like CONSTANT_NAME and not $CONSTANT_NAME meaning something defined by using define() function.

    http://us2.php.net/manual/en/language.constants.php

    phper's points are still valid in why they're useful though and another thing about them that is useful is they're like superglobals. You can use them anywhere in your script such as in a function without having to 'global' them unlike a $variable.
     
    zerxer, Jan 7, 2009 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    I find constants useless most of the times, they are annoying when dealing with strings as you need to concatenate them. "String1".CONSTANT."String2" But they are good with dealing with oop and recursive functions.
     
    Kaizoku, Jan 7, 2009 IP
  5. joomliste2000

    joomliste2000 Guest

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Constants are used for code optimization.
     
    joomliste2000, Jan 7, 2009 IP
  6. crivion

    crivion Well-Known Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    160
    Digital Goods:
    3
    #6
    crivion, Jan 7, 2009 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Useful for data which will not change (e.g. Database details), and which you need to access from all scopes (all constants are superglobal.)

    I find that using constants along with config.ini-esque files makes your code easier to pick up, and easier to alter any constant name.

    There are also useful magic constants defined by: __CONSTANTNAME__

    A list of which are here: (Please note some of the latest ones have only been added as recently as PHP 5.3)

    __LINE__
    __FILE__
    __DIR__
    __FUNCTION__
    __CLASS__
    __METHOD__
    __NAMESPACE__

    Dan
     
    Danltn, Jan 7, 2009 IP
  8. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Actually since the topic starter mentioned he/she just started learning, I assumed he/she was referring to the PHP5-style constants (as a class property) rather than the old-school define()'d ones (which can/should now be avoided whenever possible).
    e.g.:
    class MyClass {
       const $MY_CONSTANT;
      ...
    }
    PHP:
     
    phper, Jan 7, 2009 IP
  9. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Why should they be avoided? They're still very useful for some things. Who wants to make a class just to store constant variables? You'd then have to global your class in one of your external functions so that you can access that class's constant. When I was making myspace add sites, for example, I set MySpace's generic profile URL and add URL as constants (PROFILE_URL, ADD_URL) then just had to concatenate a profile ID at the end which was always in some local variable.
     
    zerxer, Jan 7, 2009 IP
  10. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Agreed, these shouldn't be avoided by any means, exceptionally useful.
     
    Danltn, Jan 7, 2009 IP
  11. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #11
    They should be avoided because they are ugly :)

    With them being "superglobals", there is always a risk of defining different things with the same name. Often to reduce the risk people tend to use longer and longer name, which makes it ugly and still the risk of name collision still exists.

    You don't usually need to make a class just to store constants. You would add the constants to the relevant classes that you should already have, if you design your code in a good object-oriented fashion.
     
    phper, Jan 7, 2009 IP
  12. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #12
    For small scripts people prefer procedural, it is more faster and efficient.
     
    Kaizoku, Jan 7, 2009 IP
  13. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Not really. I would never define a constant from within a function or anything. I think constants should only be defined at the beginning of the script or in a config file or something similar. Should be no reason you ever have name collision. Besides, I've never actually tried this, but wouldn't it error if you try to define a constant that's already defined? If not, and if you're really worried about it, use the defined() function to check if it already exists.
     
    zerxer, Jan 7, 2009 IP
  14. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #14
    In your myspace example you created a constant named ADD_URL (presumably for the URL to add a site). Is there not a possibility someone (or even yourself) in the future (or probably already did) work with adding a friend, adding a message, etc and decide to choose the same constant name ADD_URL to define the URL?

    Using define() in a config file. You would end up having a large list of constants that may not be related. Looking for a constant would involve scanning through the long list.

    Why bother making it uglier? Class constants got added to PHP5 for very good reasons, why not make use of the improvement?

    FYI, regarding error when there's duplicates, define() would throw a PHP Notice (which often gets ignored or even not displayed at all), while with class constants it'd quit with fatal error (much safer).
     
    phper, Jan 7, 2009 IP
    tarponkeith likes this.
  15. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #15
    If you're bothering to use classes, your site must be awfully big. I don't really want my site's script quitting with fatal errors.

    I don't really think class constants were added as a replacement/improvement for actual global constants.. If anything, they're there because in any other OOP language, such as C or even VB, there's constants in classes. Might as well make it like all the others so it's just as useful. Besides, the way you set variables at the beginning of classes is a lot different than how you set them outside of a class. Outside, you could just make a variable by doing $foo = bar(); and it'd make $foo's value whatever bar() returns. Inside, you'd have to do var $foo = "some static string";. That doesn't mean class VARs are better than outside variables just like class CONSTs aren't better than outside constants.

    Anyways, in my myspace example, they're these:
    DEFINE("PROFILE_URL", "http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendID=");
    DEFINE("ADD_URL", "http://messaging.myspace.com/index.cfm?fuseaction=invite.addfriend_verify&friendID=");

    They're the URLs used to view someone's profile or add them to your friend's list, both through MySpace. I don't really see anyone else coming along and modifying my script and reusing those names, especially when I have them defined at the very top of my main include file.
     
    zerxer, Jan 7, 2009 IP