You are here: Re: static member of a class and inheritance « PHP Programming Language « IT news, forums, messages
Re: static member of a class and inheritance

Posted by Oli Filth on 09/25/06 17:17

giacomo.galilei@gmail.com said the following on 25/09/2006 17:55:
> I have this piece of code:
>
>
> abstract class Kaos
> {
> static $foo;
> abstract function MyFunc();
> }
>
> ..
> $bar = "aaa";
> class Kronos extends Kaos
> {
> function MyFunc() {
> echo self::$foo;
> }
> }
> Kronos::$foo = $bar;
>
> ..
> $bar = "bbb";
> class Zeus extends Kronos
> {
> function MyFunc() {
> parent::MyFunc();
> echo self::foo;
^
^
Where is the $ sign here??
> }
> }
> Zeus::$foo = $bar;
>
>
> At this point i would like that this code to show "aaabbb", but it
> shows "bbb";
>
> $a = new Zeus();
> $a -> MyFunc();
>
> Why ? I think that i have misunderstood the meaning of 'static' keyword
> for a class member, but i need a field that has the same name in each
> class, but in the calls of Kronos (of a Zeus object) its value is "aaa"
> and in the calls of Zeus its value is "bbb".
> How can i do this ? Is possible ?

Not this way. There is only instance of $foo, contained as a static
member of Kaos. The behaviour you are getting is similar to that in C++
and Java.

You could have a separate static $foo declared in each class; however,
this too has limitations.

What exactly do you need this for? i.e. how are you using $foo? There
may be a more suitable solution to your problem.



--
Oli

 

Navigation:

[Reply to this 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

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