473,761 Members | 6,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2704
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******@hotmai l.com (mrbog) wrote in
news:cb******** *************** ***@posting.goo gle.com:
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 "undiscipli ned" 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******@hotmai l.com (mrbog) wrote in
news:cb******** *************** ***@posting.goo gle.com:
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******@hotmai l.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************ ******@usenetve rwaltung.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************ ******@usenetve rwaltung.org> wrote in message news:<c0******* **@usenet.esken .de>...
Matthias Esken <mu************ ******@usenetve rwaltung.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******@hotma il.com> napisal w wiadomosci
news:cb******** *************** ***@posting.goo gle.com...
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1834
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
2689
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" seems to be recognised by Access in at least three cases:- 1) "Global Const". Recently someone in this group helped me resolve a problem, and it involved the use of a Global Const. By Googling "Global Const", I got plenty of hits -- but they...
5
3493
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 scope. If you have a variable whose scope is file scope in another translation unit, you have to provide a local declaration to access that variable from the other translation unit. Also, I don't see "global variable" used once in the standard....
9
2491
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 is, do "global" and "::" always go together? Is there any other use for these operators, than as a pair? TIA,
4
3536
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 myVar as string, etc. It seems to me that the scope and duration are the same, as they both are there while the application is running, and both go away when it quits. I presume that one difference is that the application state can be "flushed," such...
2
3711
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 do I ensure that changes made to the "Global.asax.vb" file are immediately reflected in the "Global.asax" file? After I change to the "Global.asax.vb" file, the "Global.asax" file date modified does not change and I do not see the updated values...
3
3947
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 anywhere in the code behind page and in the class files, but when I try to call it from an aspx page, it raises an error. <%=myvar%> in mypage.aspx raise an error : the name "myvar" is not declared.
4
5927
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 Codebehind="Global.asax.cs" Inherits="WebApplication.Global" Language="C#" %> I found numerous references to this problem on the web and tried what seems like a few (tens) suggestions, at least the ones I was able to understand. But alas no luck. Pleeeeeeeease...
4
3411
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 time. Here is a snippet of example code: def foo (a): global p global a p = a + 3 #here "a" will be reference to the global one a = 1
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.