SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How should I define constants within an object?

    Hello,

    What the title says...

    I'm not sure if I should do it in the constructor or not, outside the class (but I'd rather not, those constants are really meant only for that class). Maybe it shouldn't be constants, but simply variables?

  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)
    It depends entirely on the context. Can you give an example (preferably usage example) of the kind of constant you're referring to?

    I.e. there are constants for default settings (e.g. Settings:efaultModule or something) , constants referring to values (e.g. $Dave->setTieColour(Colour::Blue) or using binary flags, $Dave->setTieStyle(Ties::Blue | Ties::Stripy)), etc.
    Last edited by Jake Arkinstall; Feb 1, 2012 at 14:48. Reason: Mindless incorrect wordage going on there
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    698
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)

  4. #4
    Non-Member
    Join Date
    Jan 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since the constants are for the class only, why not just declare them as one of the class atrributes?

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If it's a constant for that class then you shouldnt use variables which are not constant but variable

    You can set class constants like this: (taken from http://php.net/manual/en/language.oop5.constants.php as mentioned earlier)
    PHP Code:
    class MyClass
    {
        const 
    constant 'constant value';

        function 
    showConstant() {
            echo  
    self::constant "\n";
        }



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
  •