require/include: constant vs. variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pimmy
    New Member
    • Nov 2007
    • 9

    #1

    require/include: constant vs. variable

    Hi,

    I am stuck with a silly problem which I cant resolve. Please look at the example:

    settings.php
    [code=php]
    <?php
    define('VALUE_S TRING', 'This is my string');
    $VALUE_ARRAY = array('This', 'is', 'my', 'array');
    ?>
    [/code]

    values.class.ph p
    [code=php]
    <?php
    Class Values
    {
    /* Here I have some constructor */

    public function getValues() {

    error_log("Firs t: " . VALUE_STRING);
    error_log("Seco nd: " . implode(', ', $VALUE_ARRAY));
    }
    ?>
    [/code]
    index.php
    [code=php]
    <?php
    $values = new Values();
    $values->getValues();
    ?>
    [/code]

    When I execute index.php I get the following in the error log:
    Code:
    [Fri Nov 02 11:57:59 2007] [error] [client 9.196.182.69] First:  This is my string
    [Fri Nov 02 11:57:59 2007] [error] [client 9.196.182.69] PHP Notice:  Undefined variable: VALUE_ARRAY in /usr/local/apache/htdocs/values.class.php on line 9
    [Fri Nov 02 11:57:59 2007] [error] [client 9.196.182.69] PHP Warning:  implode() [<a href='function.implode'>function.implode</a>]: Bad arguments. in /usr/local/apache/htdocs/values.class.php on line 9
    [Fri Nov 02 11:57:59 2007] [error] [client 9.196.182.69] Second:
    Does anyone know why I cant get the array values, but I can get the constant?

    Cheers,

    Pimmy
    Last edited by Atli; Nov 2 '07, 04:12 PM. Reason: Added [code] tags.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    A constant has global scope whereas variables and arrays do not.

    Comment

    • pimmy
      New Member
      • Nov 2007
      • 9

      #3
      Originally posted by code green
      A constant has global scope whereas variables and arrays do not.
      What about having inlude/require line at the beginning?

      values.class.ph p
      [code=php]
      <?php
      require_once(se ttings.php);

      Class Values
      {
      /* Here I have some constructor */

      public function getValues() {

      error_log("Firs t: " . VALUE_STRING);
      error_log("Seco nd: " . implode(', ', $VALUE_ARRAY));
      }
      ?>
      [/code]

      I thought that this would be enough to make the array visible to the function, but aparently it makes no difference. Still get the same error.

      Cheers,

      Pimmy
      Last edited by Atli; Nov 2 '07, 04:13 PM. Reason: Added [code] tags.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        What about having inlude/require line at the beginning?
        Any file that includes settings.php can see the array, but a function or class is a seperate entity again.
        If you want a function or class method to see the array it has to be included inside the function.
        But normally you would simply pass the variable name in to the function as a parameter

        Comment

        • pimmy
          New Member
          • Nov 2007
          • 9

          #5
          It is quite interesting, but this doesnt work either:
          Code:
          <?php
          Class Values
          {
          /* Here I have some constructor */
          
          public function getValues() {
          
          require_once(settings.php);
          
          error_log("First: " . VALUE_STRING);
          error_log("Second: " . implode(', ', $VALUE_ARRAY));
          }
          ?>

          Comment

          • pimmy
            New Member
            • Nov 2007
            • 9

            #6
            Quite interesting, but this doesnt work either:
            Code:
            <?php
            Class Values
            {
            /* Here I have some constructor */
            
            public function getValues() {
            
            require_once(settings.php);
            
            error_log("First: " . VALUE_STRING);
            error_log("Second: " . implode(', ', $VALUE_ARRAY));
            }
            ?>

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Hi pimmy.

              Please use [code] tags when posting code. It makes it much easier to understand your code.

              As to your problem.
              You could use the global keyword.

              Comment

              • pimmy
                New Member
                • Nov 2007
                • 9

                #8
                Originally posted by Atli
                Hi pimmy.

                Please use [code] tags when posting code. It makes it much easier to understand your code.

                As to your problem.
                You could use the global keyword.

                Ooops, sorry. Rushed into the forum without checking the features.
                Thanks for the tip, I will give the GLOBAL flag a try.

                cheers,
                Pimmy

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  [PHP]<?php
                  Class Values
                  {
                  /* Here I have some constructor */

                  public function getValues() {

                  require_once(se ttings.php);

                  error_log("Firs t: " . VALUE_STRING);
                  error_log("Seco nd: " . implode(', ', $VALUE_ARRAY));
                  }
                  ?>[/PHP]I can't see why this won't work, unless settings.php is included eleswhere in the script.
                  I think 'once' has global scope throughout the script.

                  Comment

                  • pimmy
                    New Member
                    • Nov 2007
                    • 9

                    #10
                    For some reason it behaves like that. I've tried only require as well, and got the error for already defined constants.

                    I will try GLOBAL, looks promissing.

                    Have a great weekend guys.

                    Comment

                    • code green
                      Recognized Expert Top Contributor
                      • Mar 2007
                      • 1726

                      #11
                      When I was at universtity use of globals was punishable by public flogging.
                      C; C++; VB etc

                      Comment

                      Working...