SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    const vs define()

    Hey guys, could someone explain what the difference between using const vs define for class variables?

    ie.

    Code:
    const USERNAME;
    
    vs define('USERNAME', 'blah');
    also why would you use one over the other? Thanks.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    define() is used for general constant naming, e.g.:
    PHP Code:
    <?php
    define
    ('MESSAGE''Hello World!');
    echo 
    MESSAGE;
    Whereas const is generally used in classes (a definition is automatically static):
    PHP Code:
    <?php
    class Error{
        const 
    ERROR_FILENOTFOUND 1ERROR_PARSEERROR 2ERROR_PERMISSION 4ERROR_OTHERERROR 8;
        private 
    $errorType;
        function 
    __Construct($errorType 1){
            
    $this->errorType $errorType;
        }
        function 
    getError(){
            return 
    $this->errorType;
        }
    }
    $myError = new Error(Error::PERMISSION);
    echo 
    $myError->getError();
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll be honest, I didn't even know about const, so I'm guessing it's a PHP5 thing?

    I'm only just getting into PHP5 since my host updated...

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP 5 thing indeed.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm only just getting into PHP5 since my host updated...
    Well, you have had 4 years to do that
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP 5.3 and namespaces, const is given more use.
    PHP Code:
    namespace Some;
    const 
    THING 'something';

    //----

    print Some::THING
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This question intrigues me
    Is there an actual difference with the two? Other then the superficial?
    chrome is a wrapper that combines a browser with spyware

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    No difference other then grouping.
    In the case of class constants its easier to track those down using reflection then trying to find those in the global constant store. (get_defined_constants)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So the const is only global within a class, while a the define() is global all over the board?
    chrome is a wrapper that combines a browser with spyware

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Nope, both are fully global, it is just that class/namespace constants are prefixed by there class/namespace which can help to avoid naming conflicts or help with organization.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  11. #11
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, got it
    chrome is a wrapper that combines a browser with spyware

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For example PDO::FETCH_ASSOC actually has the value 2. As long as it's unique, it doesn't matter what value you use because both reference it as FETCH_ASSOC.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •