PHP'de OOP'de Statik Metotlar ve $this
Statik metotların içinde $this erişilemez.
Bunun nedeni, statik metotların
bir nesne bağlamı dışında çağrılabilmesi,
sadece sınıf adına başvurarak.
Normal metotlarda hem statik, hem de normal özellikler ve metotlar mevcuttur.
Örneklerle inceleyelim. Normal bir metot örneği:
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public function method()
{
echo self::$staticProperty; // 'static' yazdıracak
echo $this->usualProperty; // 'usual' yazdıracak
}
}
$test = new Test;
$test->method();
?>
Statik bir metot örneği:
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public static function method()
{
echo self::$staticProperty; // 'static' yazdıracak
echo $this->usualProperty; // hata verecek
}
}
$test = new Test;
$test::method();
?>