Page 1 of 1

Global objects?

Posted: Fri Jun 27, 2003 2:40 pm
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.

Re: Global objects?

Posted: Sat Jun 28, 2003 8:25 am
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