homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
variables outside class
Sandro87




msg:4180662
 4:20 pm on Aug 2, 2010 (gmt 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.

 

Matthew1980




msg:4180715
 6:24 pm on Aug 2, 2010 (gmt 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

coopster




msg:4180828
 10:43 pm on Aug 2, 2010 (gmt 0)

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.

Sandro87




msg:4180850
 11:30 pm on Aug 2, 2010 (gmt 0)

Perfect guys! thanks!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved