Akses ke static melalui kelas dan objek dalam OOP di PHP
Properti dan metode statis dapat diakses baik melalui kelas, maupun melalui variabel yang berisi objek kelas.
Mari kita lihat contohnya.
Misalkan kita memiliki kelas Test
dengan properti statis:
<?php
class Test
{
public static $property = 'static';
}
?>
Tampilkan nilai properti statis, dengan mengakses kelas:
<?php
echo Test::$property;
?>
Sekarang tampilkan nilai properti statis, dengan mengakses objek kelas:
<?php
$test = new Test;
echo $test::$property;
?>
Diberikan kelas berikut dengan metode statis:
<?php
class Test
{
public static function show()
{
return '+++';
}
}
?>
Panggil metode ini sebagai metode kelas, dan sebagai metode objek.