473,322 Members | 1,417 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,322 software developers and data experts.

the global key word

Hello fellow programmers,

Recently I've run into an interesting bug? or possibly my own
ignorance. I wrote a class (php 4) and in the constructor made an
array global. Now the array is included into the file as a
configuration mechanism. However, the constructor for what ever reason
does not recognize the imported variable. Here's a summarized version
of the source:

config.inc:

$db['user'] = "foo";
$db['pass'] = "bar";

class.db.php:

require_once('config.inc');

Class db
{
var $user
var $pass

function db()
{
global $db;

}
}
After that point initialization occurs but $db never gets read into
the internal variables... can anyone explain this seemingly anomolous
event to me?

Jun 25 '07 #1
2 1776
On Jun 25, 7:39 pm, Manny <thebar...@gmail.comwrote:
Hello fellow programmers,

Recently I've run into an interesting bug? or possibly my own
ignorance. I wrote a class (php 4) and in the constructor made an
array global. Now the array is included into the file as a
configuration mechanism. However, the constructor for what ever reason
does not recognize the imported variable. Here's a summarized version
of the source:

config.inc:

$db['user'] = "foo";
$db['pass'] = "bar";

class.db.php:

require_once('config.inc');

Class db
{
var $user
var $pass

function db()
{
global $db;

}

}

After that point initialization occurs but $db never gets read into
the internal variables... can anyone explain this seemingly anomolous
event to me?
Why would it get read into internal variables? In the code above you
never tell it to. Perhaps you meant:

function db() {
global $db;
$this->user = $db['user'];
$this->pass = $db['pass'];
}

Although I would add that you shouldn't really do this. It would be
preferable to do something like this instead:

function db($user, $pass) {
$this->user = $user;
$this->pass = $pass;
}

And pass in the values appropriately when you instantiate the object.

Jun 26 '07 #2
..oO(Captain Paralytic)
>Does config.inc have a global $db; instruction too?
Why? Outside a function or class everything is global.

Micha
Jun 26 '07 #3

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

Similar topics

1
by: rwalker | last post by:
Error number 429 - COM object with CLSID {000209F0-0000-0000-C000-000000000046} is either not valid or not registered. Intermittent occurance of this error. Using Word.Global to run word macro...
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"...
0
by: nycers_developer | last post by:
Error number 429 - COM object with CLSID {000209F0-0000-0000-C000-000000000046} is either not valid or not registered. Intermittent occurance of this error. Using Word.Global to run word macro...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
0
by: Emily | last post by:
Imagine a world where everybody shares and has faith in each other. We have all struggled at one time or another with our jobs or careers and have wondered if there was a better way to make a...
11
by: Sylvia A. | last post by:
How can I define global classes in web application ? Classes can be set to session variables ? Thanks
8
TimNick90
by: TimNick90 | last post by:
I am making a hangman program for school that uses a function for the player to input their guess and a seperate function to test whether or not their guessed letter is in the word. The program...
15
by: Bob | last post by:
Is there anyway to access the global object from inside a function other than doing a "var _global = this;" before declaring the function? Thanks
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
by: sinbad | last post by:
hi, I've a very large C program consisting of hundred of files; I want to know what are all the global variables defined in the program. how do i do this. thanks
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...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.