It sounds like your only other option is a singleton, which works only if 
you're comfortable having only one instance of a, ever.  The following code 
also only works in PHP 5.

class A {
  static obj;

  static instance() {
    if (! self::obj instanceof A) {
      self::obj = new A();
    }
    return self::obj;
  }
}

class B {
  function print() {
    $a =  A::instance();
    $a->print();
  }
}

(Possibly a syntax error in the above, but hopefully you get the idea.)

On Saturday 01 July 2006 04:56, sempsteen wrote:
> hi all,
> i wonder if there is a way of creating an instance of a class and
> reach it direcly from any scope in PHP4. basically what i want is:
>
> class a
> {
>    function print()
>    {
>       echo 'sth';
>    }
> }
>
> $a = new a();
>
> and use this "a" instance from anywhere ex, in a function that is a
> method of another class.
>
> class b
> {
>    function print()
>    {
>       $a->print();
>    }
> }
>
> i don't want to:
>    - declare global $foo,
>    - use pre-defined $GLOBALS variable,
>    - or use a::print
>
> thanks.

-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to