Mailinglisten-Archive |
Sebastian Mendel wrote:
> Beni Buess schrieb:
>
>> echo bar::getVar() wird dir einen error geben, da bar::getVar()
>> statisch auf die variable foo::$var zugreift, diese ist allerdings
>> nicht statisch deklariert.
>> sieht die deklaration so aus:
>> protected static $var = "value of var";
>> dann geht das ohne probleme.
>
>
> IMHO kann man auf alle Eigenschaften und Methoden statisch zugreifen,
> auch wenn diese nicht als statisch deklariert sind
das ist nicht so in php5
>
> Das Schlüsselwort static bewirkt doch lediglich das die
> Methode/Eigenschaft nicht vererbt wird und nicht im Objekt verfügbar ist.
>
>
statische variablen/methoden werden vererbt. auch in einem objekt können
die statischen variablen/methoden angesprochen werden, natürlich
statisch ;-)
<code>
class Foo
{
protected static $staticVar = 'staticVar';
protected $var = 'var';
}
class StaticBar extends Foo
{
public static function run() {
echo 'StaticBar: '.self::$staticVar.'<br/>';
//das geht nicht:
//echo 'StaticBar: '.self::$var.'<br/>';
}
}
class Bar extends Foo
{
public function run() {
echo 'Bar: '.self::$staticVar.'<br/>';
echo 'Bar: '.$this->var.'<br/>';
}
}
StaticBar::run();
$bar = new Bar();
$bar->run();
</code>
Ausgabe:
StaticBar: staticVar
Bar: staticVar
Bar: var
Gruss
Beni
____________________________________________________________
This message may contain legally privileged or confidential
information and is therefore addressed to the named persons only.
The recipient should inform the sender and delete this message,
if he/she is not named as addressee.
The sender disclaims any and all liability for the integrity
and punctuality of this message.
The sender has activated an automatic virus scanning by
Messagelabs, but does not guarantee the virus free
transmission of this message.
php::bar PHP Wiki - Listenarchive