Reply to Re: Static Functions and Inheritance

Your name:

Reply:


Posted by Rik Wasmus on 01/24/08 23:09

On Thu, 24 Jan 2008 22:48:44 +0100, Johannes Permoser <ee_pp@yahoo.de> =

wrote:

> Hi,
>
> i have a class that calls a static function:
>
> class Peter
> {
> static function foo() {echo 123;}
> =

> public static function bar ()
> {
> Peter::foo();
> }
> }
>
> $p =3D new Peter();
> $p->bar();
>
> Now i want to extend the class and override the foo() function. Howeve=
r =

> when calling bar(), it will still execute the code defined in Peter an=
d =

> not the override. I tried $this::foo(), but it doesn't work.

Peter::foo() will always call the static method from the class Peter, no=
t =

PetersChild, as it directly calls a static class, which might as well be=
=

ADifferentClassAllTogether::foo()

Now, if you'd have used 'self', some more explanation would be required:=

<?php
class Peter
{
static function foo() {echo 123;}
=

public static function bar ()
{
self::foo();
}
}
class PetersChild extends Peter
{
static function foo(){echo 456;}
}

PetersChild::bar();
?>
Result: still 123
However, some juicy details:
<?php
class Peter
{
static function foo() {echo 123;}
=

public static function bar ()
{
self::foo();
}
public function foz(){
self::foo();
$this->foo();
}
}
class PetersChild extends Peter
{
static function foo(){echo 456;}
}
$a =3D new PetersChild();
$a->foz();
?>
Results in '123456'.

In short: self in a class is solved at compilation as far as I gather, a=
nd =

hence will point to the parent class, not the child class. $his is an =

instance of a class, and its methods/function are more clear. What some =
=

people want is late static binding, which will be possible from PHP5.3 =

using the static::, see =

http://nl3.php.net/manual/en/language.oop5.late-static-bindings.php
-- =

Rik Wasmus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация