Welcome to WebmasterWorld Guest from 54.145.58.37

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

variables outside class

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

Junior Member

5+ Year Member

joined:May 16, 2009
posts: 86
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


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)

Administrator

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

joined:July 31, 2003
posts:12541
votes: 1


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)

Junior Member

5+ Year Member

joined:May 16, 2009
posts:86
votes: 0


Perfect guys! thanks!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members