Connecting Tech Pros Worldwide Help | Site Map

What's the correct syntax for calling a static method in PHP5

  #1  
Old July 17th, 2005, 03:29 AM
Chung Leong
Guest
 
Posts: n/a
Say I have a class in PHP5 that looks like this:

class Hello{

function Moo() {
}

static function Boo() {
}

static function Loo() {
}
}

From within Moo(), how would I invoke Boo()? $this->Boo() seems to work, but
doesn't feel right. And if I'm in Loo(), there's no $this.

Is there a pseudo-classname akin to "parent" but refers to the current class
that can be used, or do we have to type in the class name everytime we call
a statis method?


  #2  
Old July 17th, 2005, 03:29 AM
Agelmar
Guest
 
Posts: n/a

re: What's the correct syntax for calling a static method in PHP5


Can't you just use Boo()? (That's how one would do it in C++, haven't really
looked into PHP5 that heavily yet.)

Chung Leong wrote:[color=blue]
> Say I have a class in PHP5 that looks like this:
>
> class Hello{
>
> function Moo() {
> }
>
> static function Boo() {
> }
>
> static function Loo() {
> }
> }
>
> From within Moo(), how would I invoke Boo()? $this->Boo() seems to
> work, but doesn't feel right. And if I'm in Loo(), there's no $this.
>
> Is there a pseudo-classname akin to "parent" but refers to the
> current class that can be used, or do we have to type in the class
> name everytime we call a statis method?[/color]


  #3  
Old July 17th, 2005, 03:29 AM
Chung Leong
Guest
 
Posts: n/a

re: What's the correct syntax for calling a static method in PHP5


Ok, I found the keyword through a little bit of trial and error. It's
"self":

class Hello {

function Moo() {
self::Boo();
}

static function Boo()
echo "Boo";
}

static function Loo() {
self::Boo();
}
}

Boo() alone would trigger an undefined function error.

Uzytkownik "Agelmar" <ifetteNOSPAM@comcast.net> napisal w wiadomosci
news:bubvt9$g87ss$1@ID-30799.news.uni-berlin.de...[color=blue]
> Can't you just use Boo()? (That's how one would do it in C++, haven't[/color]
really[color=blue]
> looked into PHP5 that heavily yet.)
>
> Chung Leong wrote:[color=green]
> > Say I have a class in PHP5 that looks like this:
> >
> > class Hello{
> >
> > function Moo() {
> > }
> >
> > static function Boo() {
> > }
> >
> > static function Loo() {
> > }
> > }
> >
> > From within Moo(), how would I invoke Boo()? $this->Boo() seems to
> > work, but doesn't feel right. And if I'm in Loo(), there's no $this.
> >
> > Is there a pseudo-classname akin to "parent" but refers to the
> > current class that can be used, or do we have to type in the class
> > name everytime we call a statis method?[/color]
>
>[/color]


Closed Thread