469,081 Members | 1,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,081 developers. It's quick & easy.

Permanently "global" ?

I have an array/hash that stores path information for my app. As in,
what directory this is in, what directory that's in, what the name of
the site is, what the products are called, etc. It's called $glb.

So, every function so far looks like this:

function something() {
global $glb;
}

over and over and over

How do I make $glb just always be global? I'm assuming there's a way
to do this, otherwise let me know and I'll parachute out of this
language and run back home to perl.

thanks
mrb


------------------------------------------
Signature:
Never buy the services of newsfeed.com. I am a paying customer but
I'm using google to post messages, so that I can avoid their damn
advertisement showing up in every post I make.
------------------------------------------
Jul 17 '05 #1
11 2420
mrbog wrote:
So, every function so far looks like this:

function something() {
global $glb;
}

over and over and over

How do I make $glb just always be global? I'm assuming there's a way

(snip)
You have the permanently "global" array $GLOBALS, so you may do

<?php
function something() {
$glb = $GLOBALS['glb'];
// do whatever you want with $glb, but don't forget it is a local
// variable. If you want to change it in global scope use the full
// variable.

$glb['dirname'] = 'changed inside something()'; // does not work!
$GLOBALS['glb']['appname'] = 'changed inside something()'; // this works
}

function otherthing() {
$glb = $GLOBALS['glb'];
echo 'dirname = ', $glb['dirname'], "\n";

// or, if you don't want de declare a local variable
echo 'appname = ', $GLOBALS['glb']['appname'], "\n";
}

$glb['dirname'] = 'globally set';
$glb['appname'] = 'globally set';
otherthing();
something();
otherthing();
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
dt******@hotmail.com (mrbog) wrote in
news:cb**************************@posting.google.c om:
I have an array/hash that stores path information for my app. As in,
what directory this is in, what directory that's in, what the name of
the site is, what the products are called, etc. It's called $glb.

So, every function so far looks like this:

function something() {
global $glb;
}

over and over and over

How do I make $glb just always be global? I'm assuming there's a way
to do this, otherwise let me know and I'll parachute out of this
language and run back home to perl.


Hmm. If you had crossposted this to comp.lang.perl.misc, most of the
regulars there would have told you that regardless of what language you're
using, you need to rethink your programming style if your functions/subs
have to access more than a handful of globals. It's simply a fact of life
(often learned through bitter experience) that when you have lots of
functions that communicate with each other through global variables, things
very rapidly get messy and unmaintainable because your code depends on
"action at a distance."

Six months after you write a program, you need to change the code in a
function, look at it and see a bunch of "global..." and now you have to
look at every other function that uses those variables to make sure their
behavior won't be adversely affected by any changes you make (it's even
worse in Perl, since global variables are accessible inside subs unless you
explicitly override them, so Perl regulars who have got bitten by this
sort of thing will yell at you if you don't give your variables the
narrowest scope possible). And if you ever have to ask yourself "can I
give this name to my new variable without stepping on some other code"
you're suffering from Creeping Globalism.

OOP (specifically the encapsulation aspects) is one way of getting around
the "action at a distance" problem but it's not, contrary to what some
dogmatists might say, the only way. Every common programming language, for
example, allows you to pass parameters by reference, and you can make good
use of that by passing references to shared parameters.

So basically, what you're asking for is a facility to give yourself enough
rope to hang yourself. Perl, to some extent, does that, but a lot of Perl
programmers misunderstand the reason for it. Larry Wall was aware of the
fact that there are many forms of programming discipline and that, despite
all the religious wars, what really matters is that you pick a particular
form of programming discipline and stick to it. The naive Perl programmer
regards Perl as an "undisciplined" language. The experienced Perl
programmer regards Perl as a "bring your own discipline (BYOD)" language.
The lesson here is applicable to other languages, including PHP. If your
functions are heavily dependent on global variables, you need to refactor
your code.

Jul 17 '05 #3
Pedro Graca <he****@hotpop.com> wrote in message news:<c0*************@ID-203069.news.uni-berlin.de>...
mrbog wrote:
So, every function so far looks like this:

function something() {
global $glb;
}

over and over and over

How do I make $glb just always be global? I'm assuming there's a way

(snip)
You have the permanently "global" array $GLOBALS, so you may do

<?php
function something() {
$glb = $GLOBALS['glb'];
// do whatever you want with $glb, but don't forget it is a local
// variable. If you want to change it in global scope use the full
// variable.

$glb['dirname'] = 'changed inside something()'; // does not work!
$GLOBALS['glb']['appname'] = 'changed inside something()'; // this works
}

function otherthing() {
$glb = $GLOBALS['glb'];
echo 'dirname = ', $glb['dirname'], "\n";

// or, if you don't want de declare a local variable
echo 'appname = ', $GLOBALS['glb']['appname'], "\n";
}

$glb['dirname'] = 'globally set';
$glb['appname'] = 'globally set';
otherthing();
something();
otherthing();
?>


I don't understand- there's no gain there, right? I still have to
include that extra line at the top of every one of my functions, isn't
that just as bad?

What I want to do (in a short example) is:

function something() {
print $glb[someglobal];
}

and have it print the global value. I don't want to have to do this
over and over:

function something() {
global $glb;
print $glb[someglobal];
}

How do I say in my code "Just always make $glb in the global scope,
everywhere, don't make me keep telling you."
Jul 17 '05 #4
Eric Bohlman:
dt******@hotmail.com (mrbog) wrote in
news:cb**************************@posting.google.c om:
I have an array/hash that stores path information for my app. As in,
what directory this is in, what directory that's in, what the name of
the site is, what the products are called, etc. It's called $glb.

So, every function so far looks like this:

function something() {
global $glb;
}

over and over and over

How do I make $glb just always be global? I'm assuming there's a way
to do this, otherwise let me know and I'll parachute out of this
language and run back home to perl.
Hmm. If you had crossposted this to comp.lang.perl.misc, most of the
regulars there would have told you that regardless of what language you're
using, you need to rethink your programming style if your functions/subs
have to access more than a handful of globals. It's simply a fact of life
(often learned through bitter experience) that when you have lots of
functions that communicate with each other through global variables,
things very rapidly get messy and unmaintainable because your code depends
on "action at a distance."

OOP (specifically the encapsulation aspects) is one way of getting around
the "action at a distance" problem but it's not, contrary to what some
dogmatists might say, the only way. Every common programming language,
for example, allows you to pass parameters by reference, and you can make
good use of that by passing references to shared parameters.


When you pass by reference you can alter the value of an object/entity that
might possibly be used in many other places. This does not in any way
remove you from the problem, on the contrary, it creates it. The only way
to avoid it would be to pass by *value*. This is really about programming
with side-effects and OOP is all about side-effects. Functionaly languages
try to remove the need for side-effects because they are bad. But in some
cases the side-effects are a necessary evil.
So basically, what you're asking for is a facility to give yourself enough
rope to hang yourself.


All he wanted was a simple way to share configuration data, data which is
usually common to the entire application and never changes. Now this has
the characteristics of constants, and IMO that's what he should use. I
always use constants for configuration like this because one usually only
need a handful. Constants have the same scope as superglobals and can be
directly accessed from within functions.

A second solution would be to store the data in one of the superglobals, but
in don't think that's a very good solution because the superglobals have
very specific uses, and you might confuse yourself easily if you start
putting all sorts of strange data in there.

André Nęss
Jul 17 '05 #5
dt******@hotmail.com (mrbog) schrieb:
I don't understand- there's no gain there, right? I still have to
include that extra line at the top of every one of my functions, isn't
that just as bad?

What I want to do (in a short example) is:

function something() {
print $glb[someglobal];
}

and have it print the global value. I don't want to have to do this
over and over:

function something() {
global $glb;
print $glb[someglobal];
}

How do I say in my code "Just always make $glb in the global scope,
everywhere, don't make me keep telling you."


There is no way to make $glb global. Of course you could use $_GLOBAL.
See http://www.php.net/manual/en/languag...predefined.php for
details.

Example:

$_GLOBALS['var1'] = 'foo';
$_GLOBALS['var2'] = array (
'foo' => 23,
'bar' => 41
);

function something() {
echo ($_GLOBALS['var1']);
}

function something2() {
echo ($_GLOBALS['var1']['foo']);
$_GLOBALS['var2']['bar'] += 1;
}

something();
something2();
echo ($_GLOBALS['var1']['bar']);
Regards,
Matthias
Jul 17 '05 #6
Matthias Esken <mu******************@usenetverwaltung.org> schrieb:
There is no way to make $glb global. Of course you could use $_GLOBAL.


I knew I'd make an error. :-)

The correct name of the array is $GLOBALS.

Regards,
Matthias
Jul 17 '05 #7
Matthias Esken <mu******************@usenetverwaltung.org> wrote in message news:<c0*********@usenet.esken.de>...
Matthias Esken <mu******************@usenetverwaltung.org> schrieb:
There is no way to make $glb global. Of course you could use $_GLOBAL.


I knew I'd make an error. :-)

The correct name of the array is $GLOBALS.

Regards,
Matthias


Matthias, thank you. This is exactly what I needed.
thanks much,

mrb
Jul 17 '05 #8
> Hmm. If you had crossposted this to comp.lang.perl.misc, most of the
regulars there would have told you that regardless of what language you're
using, you need to rethink your programming style if your functions/subs
have to access more than a handful of globals.


Hello?? All I want is "a handful of globals". If I cross-posted to
comp.lang.perl.misc, they'd agree vehemently. This is one of the many
absurdities of PHP.

Look, I've been writing applications professionally for ten years.
I've been writing perl since just after perl 4 was released. Don't
tell me I don't know how to write proper perl. If one of us needs a
computer science lesson, it's you.

PHP is the only language (well except for obscure languages that no
one uses) that requires the programmer to explicitly bring a global
into scope. The whole point of a global is that it's always in scope.
That's what a global is supposed to be.

And btw, perl doesn't actually have global variables. Every perl
variable is in a package. The default package is main. Go try it in
your (perl) code:

$v="wef";
print $main::v;

This could (SHOULD) be the same in PHP but PHP doesn't have
namespaces, which is why it continues to be a "toy" language, which
you can't even architect with UML.

You're telling me that unless a language forces me to explicitly bring
globals into scope, that it's "giving me enough rope to hang
myself"?!? Hmm so I guess that's true of every language except PHP,
right? Brilliant, so you've taken a design flaw in PHP and made it
into it's greatest virtue. "A flat tire stops you from speeding-
flat tires are a feature, not a flaw."

(Matthias solved it for me, thanks to Matt)

mrb
Jul 17 '05 #9
What you have are constants. In general, it's best to avoid using variables
as constants (because variables are not constant). Constants always have
global scope.

You can't create your own super globals. You can hijack one of the existing
ones though:

$_ENV = $glb;

Uzytkownik "mrbog" <dt******@hotmail.com> napisal w wiadomosci
news:cb**************************@posting.google.c om...
I have an array/hash that stores path information for my app. As in,
what directory this is in, what directory that's in, what the name of
the site is, what the products are called, etc. It's called $glb.

So, every function so far looks like this:

function something() {
global $glb;
}

over and over and over

How do I make $glb just always be global? I'm assuming there's a way
to do this, otherwise let me know and I'll parachute out of this
language and run back home to perl.

thanks
mrb


------------------------------------------
Signature:
Never buy the services of newsfeed.com. I am a paying customer but
I'm using google to post messages, so that I can avoid their damn
advertisement showing up in every post I make.
------------------------------------------

Jul 17 '05 #10
> PHP is the only language (well except for obscure languages that no
one uses) that requires the programmer to explicitly bring a global
into scope. The whole point of a global is that it's always in scope.
That's what a global is supposed to be.


And that's why $GLOBALS is there. When you work with several people,
it prevents from messing with the other people's variables given they
are local unless speficied otherwise. Also when you will re-read your
code in a year, you will know immediatly if your variable is local or
global. Btw, you could also send it as parameter of the function
instead, so that you don't need to change every occurence in all the
functions using it if you change the name of the global variable, plus
it will be easier for re-using the function in other scripts.
Jul 17 '05 #11
Uzytkownik "mrbog" <dt******@hotmail.com> napisal w wiadomosci
news:cb**************************@posting.google.c om...
PHP is the only language (well except for obscure languages that no
one uses) that requires the programmer to explicitly bring a global
into scope. The whole point of a global is that it's always in scope.
That's what a global is supposed to be.


Well, the alternative would be having to declare variables as local, like
Javascript. In C/C++ it makes sense, since you need to declare the type
anyway. But for a typeless language, PHP's arrangement is quite reasonable.
It's hilarious sometimes when I see JS code where properties of the window
object are used as looping variables.
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris Stromberger | last post: by
9 posts views Thread by Javaman59 | last post: by
2 posts views Thread by Steve | last post: by
4 posts views Thread by =?Utf-8?B?QWxleCBNdW5r?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.