Click here to get back home

question on avoiding abuse of global

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
question on avoiding abuse of global lazy 06-22-2008
Get Chitika Premium
Posted by lazy on June 22, 2008, 11:08 am
Please log in for more thread options
hi,
I have some constants defined in a php script say config.php.
I want to use the variables there defined in other scripts.
couple of questions regd that:

1. Is there an alternative to including config.php and declaring the
variables that will be used as global. This seems very inefficient.

2.Moreover these variables are constants, is there a way to make the
variables unmodifiable in config.php so that scripts that use them ,
dont overrite them. Is there a php construct which can delare a
variable as constant.

Posted by Jerry Stuckle on June 22, 2008, 11:09 am
Please log in for more thread options
lazy wrote:
> hi,
> I have some constants defined in a php script say config.php.
> I want to use the variables there defined in other scripts.
> couple of questions regd that:
>
> 1. Is there an alternative to including config.php and declaring the
> variables that will be used as global. This seems very inefficient.
>

No. For your code to know about them, you must define them somewhere.
A file which is included in others is as good as any - and quite
efficient. But efficiency shouldn't be your problem right now anyway.
Get it working; if you have performance problems. find the cause of your
performance problem and fix it. Including a configuration file will not
be the problem.

> 2.Moreover these variables are constants, is there a way to make the
> variables unmodifiable in config.php so that scripts that use them ,
> dont overrite them. Is there a php construct which can delare a
> variable as constant.
>

Check out the define().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Posted by sheldonlg on June 22, 2008, 2:24 pm
Please log in for more thread options
Jerry Stuckle wrote:
> lazy wrote:
>> hi,
>> I have some constants defined in a php script say config.php.
>> I want to use the variables there defined in other scripts.
>> couple of questions regd that:
>>
>> 1. Is there an alternative to including config.php and declaring the
>> variables that will be used as global. This seems very inefficient.
>>
>
> No. For your code to know about them, you must define them somewhere. A
> file which is included in others is as good as any - and quite
> efficient. But efficiency shouldn't be your problem right now anyway.
> Get it working; if you have performance problems. find the cause of your
> performance problem and fix it. Including a configuration file will not
> be the problem.

The OP said "alternative to including config.php". What he can do is to
declare some session variables in config.php, and then use them
elsewhere without including config.php. So, the answer to that part is
"yes".

>
>> 2.Moreover these variables are constants, is there a way to make the
>> variables unmodifiable in config.php so that scripts that use them ,
>> dont overrite them. Is there a php construct which can delare a
>> variable as constant.
>>
>
> Check out the define().
>

Posted by Jerry Stuckle on June 22, 2008, 2:26 pm
Please log in for more thread options
sheldonlg wrote:
> Jerry Stuckle wrote:
>> lazy wrote:
>>> hi,
>>> I have some constants defined in a php script say config.php.
>>> I want to use the variables there defined in other scripts.
>>> couple of questions regd that:
>>>
>>> 1. Is there an alternative to including config.php and declaring the
>>> variables that will be used as global. This seems very inefficient.
>>>
>>
>> No. For your code to know about them, you must define them somewhere.
>> A file which is included in others is as good as any - and quite
>> efficient. But efficiency shouldn't be your problem right now anyway.
>> Get it working; if you have performance problems. find the cause of
>> your performance problem and fix it. Including a configuration file
>> will not be the problem.
>
> The OP said "alternative to including config.php". What he can do is to
> declare some session variables in config.php, and then use them
> elsewhere without including config.php. So, the answer to that part is
> "yes".
>
>>
>>> 2.Moreover these variables are constants, is there a way to make the
>>> variables unmodifiable in config.php so that scripts that use them ,
>>> dont overrite them. Is there a php construct which can delare a
>>> variable as constant.
>>>
>>
>> Check out the define().
>>
>

Which means he needs a session_start() at the beginning of every page
(which has more overhead than a simple include), as well as needing to
set the values in the session. And what happens if someone doesn't
enter through the home page, i.e. through a bookmarked page? He'd have
to check EVERY page to ensure the values are set - and if they aren't
either include the file or redirect the person back to a page which does
set the values.

Plus values in the $_SESSION are NOT constant, which he asked for, also.

I could find other problems with your 'suggestion', also. But this
should be enough...

So the answer is NO.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Posted by hellbringer on June 23, 2008, 10:38 am
Please log in for more thread options
lazy schrieb:
> hi,
> I have some constants defined in a php script say config.php.
> I want to use the variables there defined in other scripts.
> couple of questions regd that:
>
> 1. Is there an alternative to including config.php and declaring the
> variables that will be used as global. This seems very inefficient.
>
> 2.Moreover these variables are constants, is there a way to make the
> variables unmodifiable in config.php so that scripts that use them ,
> dont overrite them. Is there a php construct which can delare a
> variable as constant.

Use a singleton pattern.

Similar ThreadsPosted
User tracking - abuse? June 27, 2006, 1:45 pm
Avoiding guestbook spam May 24, 2007, 4:57 am
Avoiding endless conditional statements August 26, 2005, 12:30 pm
Avoiding timeout while mass mailing March 28, 2006, 10:11 am
Using mail() and avoiding spam boxes ! February 8, 2007, 5:25 pm
Avoiding double-submissions with mail script February 1, 2006, 12:49 pm
sending an email using mail() and avoiding it to be marked as spam... November 5, 2006, 12:18 am
question about using "global" on one page then using the variable on another page April 5, 2007, 1:28 pm
using global var (global settings?) June 19, 2006, 12:56 pm
global June 29, 2005, 8:35 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap