Accessing included constants from a class method

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsainteclaire
Forum Newbie
Posts: 5
Joined: Fri Aug 28, 2009 2:38 pm

Accessing included constants from a class method

Post 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
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Accessing included constants from a class method

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Accessing included constants from a class method

Post by jackpf »

Or use the const statement if you're using >= php 5.3 :) It's way better imo.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Accessing included constants from a class method

Post 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;
 
}
}
 
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Accessing included constants from a class method

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Accessing included constants from a class method

Post 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.
Post Reply