array as “class constant”

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    array as “class constant”

    Hi,

    I’d like to have something like
    Code:
    class MyClass
    {
        const HASH = array(…);
    }
    but that’s not allowed, so my question is, should I use a standard or static property instead?
    Code:
    class MyClass
    {
        private static $HASH = array(…);
    // or
        private $HASH = array(…);
    }
  • aktar
    New Member
    • Jul 2006
    • 105

    #2
    I don't know of any other options than to use either a standard or static property

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      and which would you prefer, and why?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Perhaps if you describe your requirement we could offer some advice :)

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          ok (though it won’t make much difference)

          initially I wanted a constant (= unchangeable) array in the class, to use it in one of the methods. const HASH = array(…); would have been perfect for that purpose, but it’s not allowed, leaving me with the choice of either using protected $HASH = array(…); or protected static $HASH = array(…);. now I’d like to know, which one would be better.

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            serialize your array and put it in a constant :)

            Yes that means you can still change it when you unserialize() it. :)

            These are the odd cases where PHP does not come up to par with C++.

            Use static just so you know you should behave differently while using it, and hopefully to remember to not change it.

            Explain your situation a little bit more and I might suggest something else.



            Dan

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              serialize your array and put it in a constant :)
              very funny, indeed.

              I never though such a simple question can get so complicated.
              Attached Files

              Comment

              • dlite922
                Recognized Expert Top Contributor
                • Dec 2007
                • 1586

                #8
                The way you have it is how I'd do it.

                If I was hell-bent on making sure it did not change, I'd make a singleton class and make all the error-codes in there constants and its 2 methods static.

                So then I'd use it like this for your deny() function:

                Code:
                if(HTTP_Headers::isValid($code))
                
                header("HTTP/1.1 $code " . HTTP_Headers::get([$code]));
                Dan,
                [the php nut @]

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  firstly, Abstract Registry is better than Singleton.

                  secondly, too much computing for that simple purpose.

                  Comment

                  Working...