> For the complete documentation index, see [llms.txt](https://learn.devlabss.my.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.devlabss.my.id/php/bab-7-php-oop-dasar/7.4-access-modifiers.md).

# 7.4 Access Modifiers

Access modifier seperti `public`, `protected`, dan `private` menentukan siapa yang bisa mengakses anggota class.

Bagian ini membantu Anda memilih modifier sesuai kebutuhan.

### Tujuan belajar

Setelah mempelajari bagian ini, Anda diharapkan bisa:

* memahami fungsi access modifier
* membedakan `public`, `protected`, dan `private`
* memilih modifier yang sesuai untuk contoh dasar

### Kenapa modifier dibutuhkan

Tidak semua data harus bisa diakses dari luar class.

Kadang ada data yang boleh dibuka bebas. Kadang ada yang perlu dibatasi agar lebih aman dan teratur.

### `public`

`public` berarti bisa diakses dari luar class.

```php
<?php
class Siswa {
    public $nama = "Rina";
}

$siswa1 = new Siswa();
echo $siswa1->nama;
?>
```

### `private`

`private` berarti hanya bisa diakses dari dalam class itu sendiri.

```php
<?php
class Siswa {
    private $nilai = 90;
}
?>
```

Property `private` tidak bisa dipanggil langsung dari luar object.

### `protected`

`protected` berarti bisa diakses dari class itu sendiri dan class turunannya.

Untuk tahap dasar, Anda cukup memahami bahwa `protected` ada di tengah antara `public` dan `private`.

### Cara paling mudah mengingatnya

Ingat urutannya seperti ini:

* `public` paling terbuka
* `protected` lebih terbatas
* `private` paling tertutup

### Contoh sederhana

```php
<?php
class Produk {
    public $nama = "Mouse";
    private $harga = 75000;

    public function tampilHarga() {
        echo $this->harga;
    }
}

$produk1 = new Produk();
echo $produk1->nama;
?>
```

Pada contoh itu, `$nama` bisa diakses dari luar. `$harga` tidak.

### Kesalahan umum

* mengira semua property bisa diakses dari luar
* salah memilih modifier
* bingung kenapa property `private` tidak bisa dipanggil langsung

### Latihan singkat

Buat class `Akun` yang punya:

* property `username` dengan `public`
* property `password` dengan `private`

Lalu coba pahami mana yang bisa dan tidak bisa diakses dari luar.

### Poin evaluasi bab 7.4.

Pastikan Anda sudah memahami poin berikut:

* Saya paham fungsi access modifier.
* Saya tahu perbedaan `public`, `protected`, dan `private`.
* Saya bisa memilih modifier dasar untuk contoh sederhana.
* Saya paham bahwa tidak semua data harus dibuka dari luar class.

Jika semua sudah jelas, lanjut ke [7.5 Pilar Inheritance](/php/bab-7-php-oop-dasar/7.5-pilar-inheritance.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.devlabss.my.id/php/bab-7-php-oop-dasar/7.4-access-modifiers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
