473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2667
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Stromberger | last post by:
This doesn't seem like it should behave as it does without using "global d" in mod(). d = {} def mod(): d = 3 mod() print d
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
9
by: Javaman59 | last post by:
I saw in a recent post the :: operator used to reach the global namespace, as in global::MyNamespace I hadn't seen this before, so looked it up in MSDN, which explained it nicely. My question...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
2
by: Steve | last post by:
I am new to this newsgroup & to .NET in general. I have been playing around with Visual Studio .NET, building and rendering web pages using VB "code behind" files. My problem / question is; How...
3
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
4
by: =?Utf-8?B?QWxleCBNdW5r?= | last post by:
My Web application is developed in C# Visual Studio 2005 Professional. After deploying the application to the production server I am getting the following error: <%@ Application...
4
ChrisWang
by: ChrisWang | last post by:
Hi, I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.