PHP Static & Constants

Immutable & Eternal

EST. 2024

The Ancient Laws

01. Konsep Static
02. Static Property & Method
03. Class Constants (Const)
04. self:: vs $this
05. Late Static Binding
I

Static

Milik Class, Bukan Object

Apa itu Static?

Keyword static membuat property atau method menjadi milik Class itu sendiri, bukan milik instance (objek).

Artinya, kita bisa mengaksesnya TANPA membuat objek (tanpa new).

Analogi Monolith

Bayangkan sebuah Tugu Batu (Monolith) di tengah kota.

Kapan Pakai Static?

II

Implementasi

Property & Method

Static Property

Variabel yang nilainya dibagi (shared) ke semua instance.


class MathHelper {
    public static $pi = 3.14;
}

echo MathHelper::$pi; // Akses langsung pakai ::
            

Static Method

Fungsi yang bisa dipanggil tanpa instansiasi.


class MathHelper {
    public static function add($a, $b) {
        return $a + $b;
    }
}

echo MathHelper::add(5, 10); // 15
            

Scope Resolution Operator (::)

Tanda titik dua ganda :: disebut Paamayim Nekudotayim.

Digunakan untuk mengakses member static, constant, atau method parent.

Shared State

Jika satu objek mengubah static property, objek lain juga akan melihat perubahannya.


class Counter {
    public static $count = 0;
    public function __construct() {
        self::$count++;
    }
}

new Counter();
new Counter();
echo Counter::$count; // 2
            
III

Constants

Nilai Abadi

Apa itu Constant?

Nilai yang TIDAK BISA DIUBAH (Immutable) setelah didefinisikan.

Didefinisikan dengan keyword const.

Tidak perlu pakai $.

Deklarasi Constant


class Lingkaran {
    const PI = 3.14159;
    
    public function luas($r) {
        return self::PI * $r * $r;
    }
}

echo Lingkaran::PI;
            

Visibility Constant (PHP 7.1+)

Constant juga bisa punya visibility.


class Config {
    public const APP_NAME = "MyApp";
    private const API_KEY = "secret123";
}
            

Global Define vs Class Const

define()

Global scope.

Runtime definition.

const

Class scope.

Compile-time definition (lebih cepat).

IV

self:: vs $this

Menunjuk Diri Sendiri

Perbedaan Mendasar

Aturan Pakai

Gunakan $this untuk property/method biasa.

Gunakan self:: untuk static property/method dan constant.

Contoh Penggunaan


class User {
    public static $minPassLength = 8; // Static
    public $username; // Non-static

    public function check() {
        // Akses static pakai self::
        if (strlen($this->username) < self::$minPassLength) {
            return false;
        }
    }
}
            

Static Method & $this

PENTING: Di dalam static method, kita TIDAK BISA menggunakan $this.

Kenapa? Karena static method bisa dipanggil tanpa ada objeknya.

V

Late Static Binding

static:: vs self::

Masalah pada Inheritance

self:: selalu merujuk ke class dimana kode itu DITULIS, bukan class yang dipanggil saat runtime.

Contoh Masalah


class A {
    public static function who() { echo __CLASS__; }
    public static function test() { self::who(); }
}
class B extends A {
    public static function who() { echo __CLASS__; }
}

B::test(); // Output: A (Padahal kita panggil dari B)
            

Solusi: static::

Gunakan static:: untuk merujuk ke class yang MEMANGGIL method tersebut (Runtime).


class A {
    public static function test() { static::who(); }
}

B::test(); // Output: B (Benar!)
            

Kapan Pakai static::?

Gunakan saat Anda membuat method static di Parent Class yang mungkin akan di-override oleh Child Class.

Magic Constant

PHP punya constant bawaan yang nilainya berubah tergantung konteks.

Utility Class Pattern

Class yang semua isinya static method. Biasanya class ini dibuat final dan constructor-nya private agar tidak bisa di-instansiasi.

Contoh Utility Class


final class Str {
    private function __construct() {} // Prevent new

    public static function upper($str) {
        return strtoupper($str);
    }
}
            

Kelebihan & Kekurangan Static

Kelebihan

Mudah dipanggil (Global access).

Tidak perlu alokasi memori objek.

Kekurangan

Sulit di-test (Mocking susah).

Global state bisa bikin bug susah dilacak.

Kesimpulan

Terima Kasih

Abadi Selamanya.

1 / 32