Re: global class instance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You can simulate singletons in PHP 4 as well.  But you've just eliminated 
every option for accessing a non-local object inside a function.  You've got 
4 options.  Pick one, but I'm fairly certain there is no magical 5th. :-)

On Saturday 01 July 2006 13:02, sempsteen wrote:
> Thanks Larry but i'm working on PHP4. Also you don't use $a directly.
> Instead of writing
> $a =  A::instance();
> i can write:
> global $a
>
> both takes two steps which is not what i wanted.
>
> On 7/1/06, Larry Garfield <larry@xxxxxxxxxxxxxxxx> wrote:
> > 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
> > larry@xxxxxxxxxxxxxxxx          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

-- 
Larry Garfield			AIM: LOLG42
larry@xxxxxxxxxxxxxxxx		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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux