Metode Statis dan $this dalam OOP PHP

Di dalam metode statis, $this tidak dapat diakses. Hal ini terjadi karena metode statis dapat dipanggil di luar konteks objek, hanya dengan mengacu pada nama kelas.

Dalam metode biasa, baik properti statis maupun biasa serta metode dapat diakses.

Mari kita lihat contohnya. Contoh untuk metode biasa:

Test.php<?php
  class Test
  {
    public static $staticProperty = 'static';
    public $usualProperty = 'usual';
    
    public function method()
    {
      echo self::$staticProperty; // akan menampilkan 'static'
      echo $this->usualProperty;  // akan menampilkan 'usual'
    }
  }
  
  $test = new Test;
  $test->method();
?>

Contoh untuk metode statis:

Test.php<?php
  class Test
  {
    public static $staticProperty = 'static';
    public $usualProperty = 'usual';
    
    public static function method()
    {
      echo self::$staticProperty; // akan menampilkan 'static'
      echo $this->usualProperty;  // akan menghasilkan error
    }
  }
  
  $test = new Test;
  $test::method();
?>