By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,348 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,348 IT Pros & Developers. It's quick & easy.

global object from object?

P: n/a
I'm trying to set a object as global for access through out the rest
of my script ... {a basic SQL accessing object} however if I try
calling the object from with in another object it acts as if it isn't
global and hasn't been accessed at all ... weather I decalre it from
inside the object or declare it outside the object
I would rather not have to resort to the standard of refrenceing sence
I plan on having modular support and having this one object be a
global rathen then everyone refrencing it would just be easyer for
people to understandanyways heres some of the code
and mind you this is pretty basic so far
---------index.php----------------
<?php
include_once("config.php");
include_once("libs/mod.class.php");
include_once("libs/DataBase.class.php");
include_once("libs/main.class.php");
$site= new main();
$site->init();
$site->loop();
$site->close();
?>
--------end------------------------
--------main.class.php-----------
<?php
global $db;
class main {

function init () {
global $db;
$db = new DBAccess($config['database']);
}
function loop () {}
function close () {
$db->DBKill();
}
}
?>
----end-------------------------

---error------------------------
Fatal error: Call to a member function on a non-object in
main.class.php on line 11
-------------end----------------
I would rather get this finished before continuing to work on the code
sence accessing the database will lead to 90% of the work after this

and I have googled for the answer already and come up rather dry
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"WhyteWolf" <wh*******@four-twenty.com> wrote in message
news:tc********************************@4ax.com...
I'm trying to set a object as global for access through out the rest
of my script ... {a basic SQL accessing object} however if I try
calling the object from with in another object it acts as if it isn't
global and hasn't been accessed at all ... weather I decalre it from
inside the object or declare it outside the object
I would rather not have to resort to the standard of refrenceing sence
I plan on having modular support and having this one object be a
global rathen then everyone refrencing it would just be easyer for
people to understandanyways heres some of the code
and mind you this is pretty basic so far
---------index.php----------------
<?php
include_once("config.php");
include_once("libs/mod.class.php");
include_once("libs/DataBase.class.php");
include_once("libs/main.class.php");
$site= new main();
$site->init();
$site->loop();
$site->close();
?>
--------end------------------------
--------main.class.php-----------
<?php
global $db;
class main {

function init () {
global $db;
$db = new DBAccess($config['database']);
}
function loop () {}
function close () {
$db->DBKill();
}
}
?>
----end-------------------------

---error------------------------
Fatal error: Call to a member function on a non-object in
main.class.php on line 11
-------------end----------------
I would rather get this finished before continuing to work on the code
sence accessing the database will lead to 90% of the work after this

and I have googled for the answer already and come up rather dry


Variables in PHP are considered local unless declared 'global' in a given
scope; your initial 'global $db' is not required and indeed makes no sense
at file scope.

Example from the variable scope help (at
http://uk.php.net/manual/en/language...es.scope.php):
<?php
$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
}

Sum();
echo $b;
?>
Garp
Jul 17 '05 #2

P: n/a
On Wed, 30 Jun 2004 22:58:53 GMT, "Garp" <ga***@no7.blueyonder.co.uk>
wrote:

"WhyteWolf" <wh*******@four-twenty.com> wrote in message
news:tc********************************@4ax.com.. .
I'm trying to set a object as global for access through out the rest
of my script ... {a basic SQL accessing object} however if I try
calling the object from with in another object it acts as if it isn't
global and hasn't been accessed at all ... weather I decalre it from
inside the object or declare it outside the object
I would rather not have to resort to the standard of refrenceing sence
I plan on having modular support and having this one object be a
global rathen then everyone refrencing it would just be easyer for
people to understandanyways heres some of the code
and mind you this is pretty basic so far
---------index.php----------------
<?php
include_once("config.php");
include_once("libs/mod.class.php");
include_once("libs/DataBase.class.php");
include_once("libs/main.class.php");
$site= new main();
$site->init();
$site->loop();
$site->close();
?>
--------end------------------------
--------main.class.php-----------
<?php
global $db;
class main {

function init () {
global $db;
$db = new DBAccess($config['database']);
}
function loop () {}
function close () {
$db->DBKill();
}
}
?>
----end-------------------------

---error------------------------
Fatal error: Call to a member function on a non-object in
main.class.php on line 11
-------------end----------------
I would rather get this finished before continuing to work on the code
sence accessing the database will lead to 90% of the work after this

and I have googled for the answer already and come up rather dry


Variables in PHP are considered local unless declared 'global' in a given
scope; your initial 'global $db' is not required and indeed makes no sense
at file scope.

Example from the variable scope help (at
http://uk.php.net/manual/en/language...es.scope.php):
<?php
$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
}

Sum();
echo $b;
?>

I understand that unforchantly it doens't help with the problem ...
the first global $db was an atifact I forgot to remove from messing
around with this ... the fact remains that I can't call the global $db
from inside an object weather or not the global was created in the
object or not, basicly I can't access varables or objects that are
created outside the object
basicly from what I'm seeing in the testing that I'm doing is the the
refrence is local even with "global" when dealing with objects and
that I'm most likely going to have to use the refrencing anyway
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.