Statiese Metodes en $this in OOP in PHP
Binne statiese metodes is $this nie beskikbaar nie.
Dit gebeur omdat statiese metodes
buite die konteks van 'n voorwerp geroep kan word,
net deur na die klasnaam te verwys.
In gewone metodes is beide statiese, en gewone eienskappe en metodes beskikbaar.
Kom ons kyk na voorbeelde. Voorbeeld op 'n gewone metode:
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public function method()
{
echo self::$staticProperty; // sal 'static' uitvoer
echo $this->usualProperty; // sal 'usual' uitvoer
}
}
$test = new Test;
$test->method();
?>
Voorbeeld op 'n statiese metode:
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public static function method()
{
echo self::$staticProperty; // sal 'static' uitvoer
echo $this->usualProperty; // sal 'n fout gee
}
}
$test = new Test;
$test::method();
?>