Connecting Tech Pros Worldwide Forums | Help | Site Map

require/include: constant vs. variable

Newbie
 
Join Date: Nov 2007
Posts: 9
#1: Nov 2 '07
Hi,

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

settings.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. define('VALUE_STRING', 'This is my string');
  3. $VALUE_ARRAY = array('This', 'is', 'my', 'array');
  4. ?>
  5.  
values.class.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. Class Values
  3. {
  4. /* Here I have some constructor */
  5.  
  6. public function getValues() {
  7.  
  8.         error_log("First:  " . VALUE_STRING);
  9.         error_log("Second: " . implode(', ', $VALUE_ARRAY));
  10. }
  11. ?>
  12.  
index.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $values = new Values();
  3. $values->getValues();
  4. ?>
  5.  
When I execute index.php I get the following in the error log:
Expand|Select|Wrap|Line Numbers
  1. [Fri Nov 02 11:57:59 2007] [error] [client 9.196.182.69] First:  This is my string
  2. [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
  3. [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
  4. [Fri Nov 02 11:57:59 2007] [error] [client 9.196.182.69] Second:
  5.  
Does anyone know why I cant get the array values, but I can get the constant?

Cheers,

Pimmy

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#2: Nov 2 '07

re: require/include: constant vs. variable


A constant has global scope whereas variables and arrays do not.
Newbie
 
Join Date: Nov 2007
Posts: 9
#3: Nov 2 '07

re: require/include: constant vs. variable


Quote:

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.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. require_once(settings.php);
  3.  
  4. Class Values
  5. {
  6. /* Here I have some constructor */
  7.  
  8. public function getValues() {
  9.  
  10. error_log("First: " . VALUE_STRING);
  11. error_log("Second: " . implode(', ', $VALUE_ARRAY));
  12. }
  13. ?>
  14.  
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
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#4: Nov 2 '07

re: require/include: constant vs. variable


Quote:
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
Newbie
 
Join Date: Nov 2007
Posts: 9
#5: Nov 2 '07

re: require/include: constant vs. variable


It is quite interesting, but this doesnt work either:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. Class Values
  3. {
  4. /* Here I have some constructor */
  5.  
  6. public function getValues() {
  7.  
  8. require_once(settings.php);
  9.  
  10. error_log("First: " . VALUE_STRING);
  11. error_log("Second: " . implode(', ', $VALUE_ARRAY));
  12. }
  13. ?>
  14.  
Newbie
 
Join Date: Nov 2007
Posts: 9
#6: Nov 2 '07

re: require/include: constant vs. variable


Quite interesting, but this doesnt work either:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. Class Values
  3. {
  4. /* Here I have some constructor */
  5.  
  6. public function getValues() {
  7.  
  8. require_once(settings.php);
  9.  
  10. error_log("First: " . VALUE_STRING);
  11. error_log("Second: " . implode(', ', $VALUE_ARRAY));
  12. }
  13. ?>
  14.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#7: Nov 2 '07

re: require/include: constant vs. variable


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.
Newbie
 
Join Date: Nov 2007
Posts: 9
#8: Nov 2 '07

re: require/include: constant vs. variable


Quote:

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
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#9: Nov 2 '07

re: require/include: constant vs. variable


[PHP]<?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));
}
?>[/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.
Newbie
 
Join Date: Nov 2007
Posts: 9
#10: Nov 2 '07

re: require/include: constant vs. variable


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.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#11: Nov 2 '07

re: require/include: constant vs. variable


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


Similar PHP bytes