Using global within a class

This is a discussion on Using global within a class within the PHP Language forums, part of the PHP Programming Forums category; I have a class from within which other classes are called. In the constructor, I want to create an instance ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-10-2004
Martin Lucas-Smith
 
Posts: n/a
Default 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
Reply With Quote
  #2 (permalink)  
Old 08-10-2004
Jacques Tôle
 
Posts: n/a
Default Re: Using global within a class

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?
Reply With Quote
  #3 (permalink)  
Old 08-11-2004
Pieter Nobels
 
Posts: n/a
Default Re: Using global within a class

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:15 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0