PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > question on avoiding abuse of global
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
question on avoiding abuse of global

Réponse
 
LinkBack Outils de la discussion
Vieux 22/06/2008, 16h08   #1
lazy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut question on avoiding abuse of global

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.
  Réponse avec citation
Vieux 22/06/2008, 16h09   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question on avoiding abuse of global

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
==================

  Réponse avec citation
Vieux 22/06/2008, 19h24   #3
sheldonlg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question on avoiding abuse of global

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().
>

  Réponse avec citation
Vieux 22/06/2008, 19h26   #4
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question on avoiding abuse of global

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
==================

  Réponse avec citation
Vieux 23/06/2008, 15h38   #5
hellbringer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question on avoiding abuse of global

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.
  Réponse avec citation
Vieux 23/06/2008, 16h14   #6
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question on avoiding abuse of global

hellbringer wrote:
> 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.
>


And how does that solve his problem? How is he going to get the
singleton if he doesn't include the file containing the singleton -
which negates the whole purpose of his question?

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

  Réponse avec citation
Vieux 24/06/2008, 12h58   #7
Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question on avoiding abuse of global

On Jun 22, 4:08 pm, lazy <arunm...@gmail.com> 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.
>
> 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.


The clue is in your question. You say that what the config file
contains should be considered constants, but you're actually declaring
them as variables.

As you want constants you really should be using define() to declare
them.

Constants defined with define() are different from variables declared
in the global scope. Firstly their value is immutable, in other words
constant, and secondly they don't require use of the global()==
keyword in blocks that aren't in the global scope. They are
automatically usable from anywhere in your code provided the file that
holds them has been included.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h09.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15896 seconds with 15 queries