473,320 Members | 1,828 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.

Using global within a class



I have a class from within which other classes are called.

In the constructor, I want to create an instance of a database connection,
so that this database can be called elsewhere.

<?php
# Declare a class foo
class foo
{
# Constructor
function foo
{
# Load the class file
require_once ('database.class.php');

# Create a database object
$database = new database ();
}
# Function to do work
function doWork ()
{
# Get the database object
global $database;

# Call some method from within bar
echo $database->doSomethingElse ();
}
}
?>

If I now create an instance of that object:

<?php
# Create a foo
$foo = new foo ();

# Get foo to do stuff
$foo->doWork ();
?>
... this doesn't work. Instead, I get:

Fatal error: Call to a member function doSomethingElse() on a
non-object in {filename} on line {line number}
Basically the global isn't picking things up from the rest of the class.

Normally I would use the standard $this-> system, but the problem is that
accessing the database could be several levels deep.

Any idea how I can make use of a global variable without having to
continually pass the object between functions, i.e.

function doWork ($database)
echo $database->doSomethingElse ();
}

constantly?
Martin
Jul 17 '05 #1
2 2146
Am Tue, 10 Aug 2004 18:11:35 +0100 schrieb Martin Lucas-Smith:

Normally I would use the standard $this-> system, but the problem is that
accessing the database could be several levels deep.


what kind of problem would that be?
Jul 17 '05 #2
Martin Lucas-Smith wrote:


I have a class from within which other classes are called.

In the constructor, I want to create an instance of a database
connection, so that this database can be called elsewhere.


Variables in functions are destroyed when the function is finished. You
have to define your variable outside the class and do global $database;
everywhere you need it. It also might be interesting to (re)read the
part about scopes in the PHP Manual.

--
Pieter Nobels
Jul 17 '05 #3

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

Similar topics

5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
15
by: Relaxin | last post by:
How can I have a variable that has been initialized and set to a value within one source file (*.cs), and have access to that same variable in other files of the same project? NOTE: That...
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
8
by: Thomas Coleman | last post by:
Ok, I've obviously discovered that Global.aspx has been completely changed in ..NET 2.0. However, I haven't figured out how to declare a constant that's available to any page in my application...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
2
by: ryan_melville | last post by:
Hi, Should I put the operator<<() for my class (which is in a namespace) in the namespace or make it global? If I understand the lookup rules correctly: If I make it global, it may be hidden...
11
by: Sylvia A. | last post by:
How can I define global classes in web application ? Classes can be set to session variables ? Thanks
2
by: RgeeK | last post by:
I'm seeing something which make me think I'm missing something about how global var's behave. I've defined a global string, right at the start of my .py file. outXMLfile = "abc" I define a...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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)...
0
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...
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...

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.