require/include: constant vs. variable | Newbie | | Join Date: Nov 2007
Posts: 9
| |
Hi,
I am stuck with a silly problem which I cant resolve. Please look at the example:
settings.php -
<?php
-
define('VALUE_STRING', 'This is my string');
-
$VALUE_ARRAY = array('This', 'is', 'my', 'array');
-
?>
-
values.class.php -
<?php
-
Class Values
-
{
-
/* Here I have some constructor */
-
-
public function getValues() {
-
-
error_log("First: " . VALUE_STRING);
-
error_log("Second: " . implode(', ', $VALUE_ARRAY));
-
}
-
?>
-
index.php -
<?php
-
$values = new Values();
-
$values->getValues();
-
?>
-
When I execute index.php I get the following in the error log: -
[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
|  | Expert | | Join Date: Mar 2007 Location: England
Posts: 1,083
| | | re: require/include: constant vs. variable
A constant has global scope whereas variables and arrays do not.
| | Newbie | | Join Date: Nov 2007
Posts: 9
| | | 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 -
<?php
-
require_once(settings.php);
-
-
Class Values
-
{
-
/* Here I have some constructor */
-
-
public function getValues() {
-
-
error_log("First: " . VALUE_STRING);
-
error_log("Second: " . implode(', ', $VALUE_ARRAY));
-
}
-
?>
-
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
|  | Expert | | Join Date: Mar 2007 Location: England
Posts: 1,083
| | | 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
| | | re: require/include: constant vs. variable
It is quite interesting, but this doesnt work either: -
<?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));
-
}
-
?>
-
| | Newbie | | Join Date: Nov 2007
Posts: 9
| | | re: require/include: constant vs. variable
Quite interesting, but this doesnt work either: -
<?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));
-
}
-
?>
-
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | 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
| | | 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
|  | Expert | | Join Date: Mar 2007 Location: England
Posts: 1,083
| | | 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
| | | 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.
|  | Expert | | Join Date: Mar 2007 Location: England
Posts: 1,083
| | | re: require/include: constant vs. variable
When I was at universtity use of globals was punishable by public flogging.
C; C++; VB etc
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|