Page 1 of 1
Accessing included constants from a class method
Posted: Fri Sep 04, 2009 7:30 pm
by dsainteclaire
I tried searching the forum for details on this in case someone had a similar question, but maybe my search terms weren't correct or something because I didn't find anything that would meet my needs. As a former C programmer, I have gotten used to having a separate file that's used just for constant variables, strings, just about anything that doesn't really change and would be likely be used by other files. Therefore, I tried to do something similar in PHP and created a file that just looks like
Code: Select all
<?php
$connString = 'arbitrary connection string';
.
.
.
?>
And then in another file I am including the above file. If I try and access the variables from just a normal PHP file everything seems to be kosher, but if I try and access any of them from within a class method nothing gets returned. Some of the research I did shows that it looks like I would need to include the file in each of the methods where I want access to the variables. Is there any way around that because it seems like a lot of overhead if I have 7-8 methods that all need something contained in that file? Or does PHP have a better way of doing it? I'm open to re-architecting my application if there's a better way of doing it, but I don't want my server to have to parse several thousand lines of code, if I can avoid it, just because I want to make my code modular. That's a hit my server probably can't afford to absorb.
Thnx,
David
Re: Accessing included constants from a class method
Posted: Fri Sep 04, 2009 8:32 pm
by AlanG
You said they weren't going to change the variables much at all. Your best bet is to use constants. If you use variables, you need to either use the globals array or define the variable as a global within each scope (each method and class), or you could pass the variables into the classes and methods, so best to use constants. Note that a constant's value cannot be changed once it has been defined.
Code: Select all
<?php
define('MYCONST','some data');
?>
Now whenever you need to use that, you can simply substitute MYCONST for the needed data; and it should work across all scopes.
Re: Accessing included constants from a class method
Posted: Fri Sep 04, 2009 8:56 pm
by jackpf
Or use the const statement if you're using >= php 5.3

It's way better imo.
Re: Accessing included constants from a class method
Posted: Sat Sep 05, 2009 12:24 am
by susrisha
I would suggesting using global. I have been using it for many classes i wrote. I keep all the constants in a file and include it and get the variable wherever i needed.
To give an example;
vars.php
Code: Select all
$my_global_variable="some global variable";
myclass.php
Code: Select all
include_once("vars.php");
class myclass{
function printmyglobal()
{
global $my_global_variable;
echo $my_global_variable;
}
}
Re: Accessing included constants from a class method
Posted: Sat Sep 05, 2009 6:22 am
by AlanG
susrisha wrote:I would suggesting using global. I have been using it for many classes i wrote. I keep all the constants in a file and include it and get the variable wherever i needed.
To give an example;
vars.php
Code: Select all
$my_global_variable="some global variable";
myclass.php
Code: Select all
include_once("vars.php");
class myclass{
function printmyglobal()
{
global $my_global_variable;
echo $my_global_variable;
}
}
My argument is that he will have to declare it as a global variable before he uses it, or use the globals array. With a constant he can just use it straight off the bat. I use constants in my configuration files.
Re: Accessing included constants from a class method
Posted: Sat Sep 05, 2009 6:57 am
by jackpf
You don't really want to use variables for stuff that shouldn't be changed. That's the point of constants. That, and that they're already global.