Static Function Variables and Concatenation in PHP












17














Consider the following:



$var = 'foo' . 'bar'; # Not a member of a class, free-standing or in a function.


As soon as I mark $var as static, however:



static $var = 'foo' . 'bar';


PHP (5.3.1 on a WAMP setup) complains with the following error:




Parse error: syntax error, unexpected '.', expecting ',' or ';'




It seems that the string concatenation is the culprit here.





What's going on here? Can someone explain the rules for static variables to me?










share|improve this question



























    17














    Consider the following:



    $var = 'foo' . 'bar'; # Not a member of a class, free-standing or in a function.


    As soon as I mark $var as static, however:



    static $var = 'foo' . 'bar';


    PHP (5.3.1 on a WAMP setup) complains with the following error:




    Parse error: syntax error, unexpected '.', expecting ',' or ';'




    It seems that the string concatenation is the culprit here.





    What's going on here? Can someone explain the rules for static variables to me?










    share|improve this question

























      17












      17








      17


      0





      Consider the following:



      $var = 'foo' . 'bar'; # Not a member of a class, free-standing or in a function.


      As soon as I mark $var as static, however:



      static $var = 'foo' . 'bar';


      PHP (5.3.1 on a WAMP setup) complains with the following error:




      Parse error: syntax error, unexpected '.', expecting ',' or ';'




      It seems that the string concatenation is the culprit here.





      What's going on here? Can someone explain the rules for static variables to me?










      share|improve this question













      Consider the following:



      $var = 'foo' . 'bar'; # Not a member of a class, free-standing or in a function.


      As soon as I mark $var as static, however:



      static $var = 'foo' . 'bar';


      PHP (5.3.1 on a WAMP setup) complains with the following error:




      Parse error: syntax error, unexpected '.', expecting ',' or ';'




      It seems that the string concatenation is the culprit here.





      What's going on here? Can someone explain the rules for static variables to me?







      php string concatenation static-variables






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 12 '11 at 6:33









      jklanders

      8814




      8814
























          3 Answers
          3






          active

          oldest

          votes


















          12














          The manual states, in Variables scope:




          Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.




          There is also mention of it in Static keyword:




          Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.




          Although it should be noted that a property, static or not, cannot be initialized using an expression neither.






          share|improve this answer





















          • why is this? How can I do some thing like : public static $model_path=APP_PATH."/models/";
            – Hafiz
            May 2 '12 at 20:09






          • 1




            @Hafiz: You can't, because these are evaluated at compile-time. Constants and string literals are evaluated at compile-time so static $foo = "a"; or public $foo = CONSTANT; works. Expressions, like public $foo = 1 + 2; or static $foo = 'a' . 'b'; are evaluated at runtime.
            – netcoder
            May 2 '12 at 20:24








          • 1




            In your case, you could simply define a MODEL_PATH constant. Do something like: define('MODEL_PATH', APP_PATH."models/"); then use this constant instead in your class: class Foo { public static $model_path = MODEL_PATH; }. Either that, or initialize them in the constructor or initialization method.
            – netcoder
            May 2 '12 at 20:25












          • I appreciate your reply but this way I will be such paths one thing at 2 places every time :| . May be I should use constant at all those places where I am using static variables with expressions.
            – Hafiz
            May 2 '12 at 20:47



















          4














          You can not do expressions in initializers. You can, however, do this:



          define('FOOBAR', 'foo'.'bar');
          static $var = FOOBAR;
          echo $var;


          Little known fact is that even though initializers can not contain runtime expressions, it can contain constants which can be defined and resolved at runtime. The constant has to be defined by the time $var is first used though, otherwise you'll get string identical to the constant (e.g. "FOOBAR").






          share|improve this answer





















          • mean I must need to define every thing as constant if I need to concatenate any thing? Mean every single initialization will take 2 steps?
            – Hafiz
            May 2 '12 at 20:10










          • Not every single one, just ones where you need to use expressions.
            – StasM
            May 3 '12 at 6:56



















          1














          I do this:



          class MyClass {

          static $var1;
          static $var2;
          public static function _init() {
          self::$var1 = 'slkslk' . 'sksks' . 'arbitrary' ;
          self::var2 = <<<EOT
          <root>
          <elem1>skjsksj</elem1>
          </root>
          EOT;
          }
          }
          MyClass::_init();





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4976717%2fstatic-function-variables-and-concatenation-in-php%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            12














            The manual states, in Variables scope:




            Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.




            There is also mention of it in Static keyword:




            Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.




            Although it should be noted that a property, static or not, cannot be initialized using an expression neither.






            share|improve this answer





















            • why is this? How can I do some thing like : public static $model_path=APP_PATH."/models/";
              – Hafiz
              May 2 '12 at 20:09






            • 1




              @Hafiz: You can't, because these are evaluated at compile-time. Constants and string literals are evaluated at compile-time so static $foo = "a"; or public $foo = CONSTANT; works. Expressions, like public $foo = 1 + 2; or static $foo = 'a' . 'b'; are evaluated at runtime.
              – netcoder
              May 2 '12 at 20:24








            • 1




              In your case, you could simply define a MODEL_PATH constant. Do something like: define('MODEL_PATH', APP_PATH."models/"); then use this constant instead in your class: class Foo { public static $model_path = MODEL_PATH; }. Either that, or initialize them in the constructor or initialization method.
              – netcoder
              May 2 '12 at 20:25












            • I appreciate your reply but this way I will be such paths one thing at 2 places every time :| . May be I should use constant at all those places where I am using static variables with expressions.
              – Hafiz
              May 2 '12 at 20:47
















            12














            The manual states, in Variables scope:




            Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.




            There is also mention of it in Static keyword:




            Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.




            Although it should be noted that a property, static or not, cannot be initialized using an expression neither.






            share|improve this answer





















            • why is this? How can I do some thing like : public static $model_path=APP_PATH."/models/";
              – Hafiz
              May 2 '12 at 20:09






            • 1




              @Hafiz: You can't, because these are evaluated at compile-time. Constants and string literals are evaluated at compile-time so static $foo = "a"; or public $foo = CONSTANT; works. Expressions, like public $foo = 1 + 2; or static $foo = 'a' . 'b'; are evaluated at runtime.
              – netcoder
              May 2 '12 at 20:24








            • 1




              In your case, you could simply define a MODEL_PATH constant. Do something like: define('MODEL_PATH', APP_PATH."models/"); then use this constant instead in your class: class Foo { public static $model_path = MODEL_PATH; }. Either that, or initialize them in the constructor or initialization method.
              – netcoder
              May 2 '12 at 20:25












            • I appreciate your reply but this way I will be such paths one thing at 2 places every time :| . May be I should use constant at all those places where I am using static variables with expressions.
              – Hafiz
              May 2 '12 at 20:47














            12












            12








            12






            The manual states, in Variables scope:




            Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.




            There is also mention of it in Static keyword:




            Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.




            Although it should be noted that a property, static or not, cannot be initialized using an expression neither.






            share|improve this answer












            The manual states, in Variables scope:




            Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.




            There is also mention of it in Static keyword:




            Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.




            Although it should be noted that a property, static or not, cannot be initialized using an expression neither.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 12 '11 at 6:43









            netcoder

            53k14104132




            53k14104132












            • why is this? How can I do some thing like : public static $model_path=APP_PATH."/models/";
              – Hafiz
              May 2 '12 at 20:09






            • 1




              @Hafiz: You can't, because these are evaluated at compile-time. Constants and string literals are evaluated at compile-time so static $foo = "a"; or public $foo = CONSTANT; works. Expressions, like public $foo = 1 + 2; or static $foo = 'a' . 'b'; are evaluated at runtime.
              – netcoder
              May 2 '12 at 20:24








            • 1




              In your case, you could simply define a MODEL_PATH constant. Do something like: define('MODEL_PATH', APP_PATH."models/"); then use this constant instead in your class: class Foo { public static $model_path = MODEL_PATH; }. Either that, or initialize them in the constructor or initialization method.
              – netcoder
              May 2 '12 at 20:25












            • I appreciate your reply but this way I will be such paths one thing at 2 places every time :| . May be I should use constant at all those places where I am using static variables with expressions.
              – Hafiz
              May 2 '12 at 20:47


















            • why is this? How can I do some thing like : public static $model_path=APP_PATH."/models/";
              – Hafiz
              May 2 '12 at 20:09






            • 1




              @Hafiz: You can't, because these are evaluated at compile-time. Constants and string literals are evaluated at compile-time so static $foo = "a"; or public $foo = CONSTANT; works. Expressions, like public $foo = 1 + 2; or static $foo = 'a' . 'b'; are evaluated at runtime.
              – netcoder
              May 2 '12 at 20:24








            • 1




              In your case, you could simply define a MODEL_PATH constant. Do something like: define('MODEL_PATH', APP_PATH."models/"); then use this constant instead in your class: class Foo { public static $model_path = MODEL_PATH; }. Either that, or initialize them in the constructor or initialization method.
              – netcoder
              May 2 '12 at 20:25












            • I appreciate your reply but this way I will be such paths one thing at 2 places every time :| . May be I should use constant at all those places where I am using static variables with expressions.
              – Hafiz
              May 2 '12 at 20:47
















            why is this? How can I do some thing like : public static $model_path=APP_PATH."/models/";
            – Hafiz
            May 2 '12 at 20:09




            why is this? How can I do some thing like : public static $model_path=APP_PATH."/models/";
            – Hafiz
            May 2 '12 at 20:09




            1




            1




            @Hafiz: You can't, because these are evaluated at compile-time. Constants and string literals are evaluated at compile-time so static $foo = "a"; or public $foo = CONSTANT; works. Expressions, like public $foo = 1 + 2; or static $foo = 'a' . 'b'; are evaluated at runtime.
            – netcoder
            May 2 '12 at 20:24






            @Hafiz: You can't, because these are evaluated at compile-time. Constants and string literals are evaluated at compile-time so static $foo = "a"; or public $foo = CONSTANT; works. Expressions, like public $foo = 1 + 2; or static $foo = 'a' . 'b'; are evaluated at runtime.
            – netcoder
            May 2 '12 at 20:24






            1




            1




            In your case, you could simply define a MODEL_PATH constant. Do something like: define('MODEL_PATH', APP_PATH."models/"); then use this constant instead in your class: class Foo { public static $model_path = MODEL_PATH; }. Either that, or initialize them in the constructor or initialization method.
            – netcoder
            May 2 '12 at 20:25






            In your case, you could simply define a MODEL_PATH constant. Do something like: define('MODEL_PATH', APP_PATH."models/"); then use this constant instead in your class: class Foo { public static $model_path = MODEL_PATH; }. Either that, or initialize them in the constructor or initialization method.
            – netcoder
            May 2 '12 at 20:25














            I appreciate your reply but this way I will be such paths one thing at 2 places every time :| . May be I should use constant at all those places where I am using static variables with expressions.
            – Hafiz
            May 2 '12 at 20:47




            I appreciate your reply but this way I will be such paths one thing at 2 places every time :| . May be I should use constant at all those places where I am using static variables with expressions.
            – Hafiz
            May 2 '12 at 20:47













            4














            You can not do expressions in initializers. You can, however, do this:



            define('FOOBAR', 'foo'.'bar');
            static $var = FOOBAR;
            echo $var;


            Little known fact is that even though initializers can not contain runtime expressions, it can contain constants which can be defined and resolved at runtime. The constant has to be defined by the time $var is first used though, otherwise you'll get string identical to the constant (e.g. "FOOBAR").






            share|improve this answer





















            • mean I must need to define every thing as constant if I need to concatenate any thing? Mean every single initialization will take 2 steps?
              – Hafiz
              May 2 '12 at 20:10










            • Not every single one, just ones where you need to use expressions.
              – StasM
              May 3 '12 at 6:56
















            4














            You can not do expressions in initializers. You can, however, do this:



            define('FOOBAR', 'foo'.'bar');
            static $var = FOOBAR;
            echo $var;


            Little known fact is that even though initializers can not contain runtime expressions, it can contain constants which can be defined and resolved at runtime. The constant has to be defined by the time $var is first used though, otherwise you'll get string identical to the constant (e.g. "FOOBAR").






            share|improve this answer





















            • mean I must need to define every thing as constant if I need to concatenate any thing? Mean every single initialization will take 2 steps?
              – Hafiz
              May 2 '12 at 20:10










            • Not every single one, just ones where you need to use expressions.
              – StasM
              May 3 '12 at 6:56














            4












            4








            4






            You can not do expressions in initializers. You can, however, do this:



            define('FOOBAR', 'foo'.'bar');
            static $var = FOOBAR;
            echo $var;


            Little known fact is that even though initializers can not contain runtime expressions, it can contain constants which can be defined and resolved at runtime. The constant has to be defined by the time $var is first used though, otherwise you'll get string identical to the constant (e.g. "FOOBAR").






            share|improve this answer












            You can not do expressions in initializers. You can, however, do this:



            define('FOOBAR', 'foo'.'bar');
            static $var = FOOBAR;
            echo $var;


            Little known fact is that even though initializers can not contain runtime expressions, it can contain constants which can be defined and resolved at runtime. The constant has to be defined by the time $var is first used though, otherwise you'll get string identical to the constant (e.g. "FOOBAR").







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 12 '11 at 6:49









            StasM

            8,33433981




            8,33433981












            • mean I must need to define every thing as constant if I need to concatenate any thing? Mean every single initialization will take 2 steps?
              – Hafiz
              May 2 '12 at 20:10










            • Not every single one, just ones where you need to use expressions.
              – StasM
              May 3 '12 at 6:56


















            • mean I must need to define every thing as constant if I need to concatenate any thing? Mean every single initialization will take 2 steps?
              – Hafiz
              May 2 '12 at 20:10










            • Not every single one, just ones where you need to use expressions.
              – StasM
              May 3 '12 at 6:56
















            mean I must need to define every thing as constant if I need to concatenate any thing? Mean every single initialization will take 2 steps?
            – Hafiz
            May 2 '12 at 20:10




            mean I must need to define every thing as constant if I need to concatenate any thing? Mean every single initialization will take 2 steps?
            – Hafiz
            May 2 '12 at 20:10












            Not every single one, just ones where you need to use expressions.
            – StasM
            May 3 '12 at 6:56




            Not every single one, just ones where you need to use expressions.
            – StasM
            May 3 '12 at 6:56











            1














            I do this:



            class MyClass {

            static $var1;
            static $var2;
            public static function _init() {
            self::$var1 = 'slkslk' . 'sksks' . 'arbitrary' ;
            self::var2 = <<<EOT
            <root>
            <elem1>skjsksj</elem1>
            </root>
            EOT;
            }
            }
            MyClass::_init();





            share|improve this answer


























              1














              I do this:



              class MyClass {

              static $var1;
              static $var2;
              public static function _init() {
              self::$var1 = 'slkslk' . 'sksks' . 'arbitrary' ;
              self::var2 = <<<EOT
              <root>
              <elem1>skjsksj</elem1>
              </root>
              EOT;
              }
              }
              MyClass::_init();





              share|improve this answer
























                1












                1








                1






                I do this:



                class MyClass {

                static $var1;
                static $var2;
                public static function _init() {
                self::$var1 = 'slkslk' . 'sksks' . 'arbitrary' ;
                self::var2 = <<<EOT
                <root>
                <elem1>skjsksj</elem1>
                </root>
                EOT;
                }
                }
                MyClass::_init();





                share|improve this answer












                I do this:



                class MyClass {

                static $var1;
                static $var2;
                public static function _init() {
                self::$var1 = 'slkslk' . 'sksks' . 'arbitrary' ;
                self::var2 = <<<EOT
                <root>
                <elem1>skjsksj</elem1>
                </root>
                EOT;
                }
                }
                MyClass::_init();






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 13 '12 at 4:19









                Cheeso

                134k73404632




                134k73404632






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4976717%2fstatic-function-variables-and-concatenation-in-php%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly