Welcome to WebmasterWorld Guest from 54.163.168.15

Forum Moderators: coopster & jatar k

variables outside class

   
4:20 pm on Aug 2, 2010 (gmt 0)

5+ Year Member



Hello,
im starting to use OOP in PHP.

I have a file called settings.php located in a different directory that same php settings plus some "global" variables like images dir,site name etc... that are used very often in scripts and are somehow the base configuration and will never change.

Now If I want to "call" them inside methods by including the php file in the classes page I can't since they're external data. They only way to make them work was to declare them as "global".
Is it the right way to it or it has some disadvantaged?
Is there a better way to organize this?

Be aware that those variables are also used in procedural code thus outside function/classes also.
6:24 pm on Aug 2, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there Sandro87,

From the sounds of what you are asking there are ways to accomplish this, could you post an example of code and the context to which your referring, then we can help you a little further.

From what I gather though, you have an external file that you want to make available within a class - if so you can use the __construct() function, but again, I would like to see some example before committing thought's here ;)

Another thought occurs to me, if this is a settings file, why not declare them as constants, then so long as you include the file in the header file of your site, the values will be by definition 'Global' as they are constants, therefore available throughout the script.

Cheers,
MRb
10:43 pm on Aug 2, 2010 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



the base configuration and will never change

+ PLUS +

why not declare them as constants, then so long as you include the file in the header file of your site, the values will be by definition 'Global' as they are constants, therefore available throughout the script.


... and that is how it is most often handled. Great use of constants.
11:30 pm on Aug 2, 2010 (gmt 0)

5+ Year Member



Perfect guys! thanks!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month