Re: question on avoiding abuse of global



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



Relevant Pages

  • Re: question on avoiding abuse of global
    ... 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: ...
    (comp.lang.php)
  • question on avoiding abuse of global
    ... 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: ... Is there a php construct which can delare a ...
    (comp.lang.php)
  • Re: Use of `my
    ... variables in my scripts ... There is a lot of declaring in main that should be more localized. ... for each file found gets collected so repeated collection of same path ... But there is more to scoping than subroutine and global scope. ...
    (perl.beginners)
  • Use of `my
    ... variables in my scripts ... With a script that searches out specific regex from files that may be ... There is a lot of declaring in main that should be more localized. ... I've declared `@SearchedDir' in main because I want to use it in ...
    (perl.beginners)
  • Re: question on avoiding abuse of global
    ... I have some constants defined in a php script say config.php. ... I want to use the variables there defined in other scripts. ... if you have performance problems. ... declare some session variables in config.php, and then use them elsewhere without including config.php. ...
    (comp.lang.php)