Global objects?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Global objects?

Post by EricS »

This particular project is going on a server with PHP 4.0.6.

I have an error class that handles a my errors. I'm wanting to call a object that was instanciated outside an object, from inside an object, with out passing it in.

for example

Code: Select all

<?php

global $error;

$error = new  Error();


class test
{

function foo()
     {
           $error->throwError();
     } 

}

$newTest = new test();
$newTest->foo();

?>
This does not call the throwError method. Is there anyway to do what I'm trying to do.

Thanks in advance.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Global objects?

Post by BDKR »

Well, passing it in is the most correct way, but if you don't want to do that, use the $GLOBALS array. Maybe...

Code: Select all

function foo()
    { $GLOBALS['error']->throwError(); }
...will work. It should work. I don't suggest it though.

Cheers,
BDKR
Post Reply