> 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.2-property-and-method.md).

# 7.2 Property & Method

Property menyimpan data milik object. Method menyimpan aksi yang bisa dijalankan object.

Materi ini menunjukkan hubungan keduanya dalam contoh class sederhana.

### Tujuan belajar

Setelah mempelajari bagian ini, Anda diharapkan bisa:

* memahami fungsi property
* memahami fungsi method
* membuat class sederhana yang punya data dan aksi

### Apa itu property

Property adalah data yang dimiliki object.

Contohnya pada object `Siswa`, property bisa berupa:

* nama
* kelas
* jurusan

### Apa itu method

Method adalah aksi yang bisa dilakukan object.

Contohnya:

* menampilkan biodata
* menghitung nilai
* memberi status lulus

### Contoh dasar

```php
<?php
class Siswa {
    public $nama = "Rina";
    public $kelas = "XI RPL 1";

    public function tampilData() {
        echo $this->nama . " - " . $this->kelas;
    }
}

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

### Penjelasan bagian penting

Pada contoh itu:

* `$nama` dan `$kelas` adalah property
* `tampilData()` adalah method
* `$this` dipakai untuk mengakses property milik object itu sendiri

### Mengubah isi property

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

$siswa1 = new Siswa();
$siswa1->nama = "Andi";

echo $siswa1->nama;
?>
```

### Kenapa property dan method penting

Dengan keduanya, data dan perilaku bisa dikelompokkan dalam satu tempat.

Ini membuat program lebih rapi dibanding data dan fungsi yang tersebar.

### Kesalahan umum

* bingung membedakan property dan variabel biasa
* lupa memakai `$this` di dalam method
* mengira method dipanggil tanpa object

### Latihan singkat

Buat class `Kendaraan` yang punya:

* property `merk`
* property `warna`
* method untuk menampilkan keduanya

### Poin evaluasi bab 7.2.

Pastikan Anda sudah memahami poin berikut:

* Saya paham property menyimpan data object.
* Saya paham method menyimpan aksi object.
* Saya tahu fungsi `$this`.
* Saya bisa membuat class sederhana dengan property dan method.

Jika semua sudah jelas, lanjut ke [7.3 Constructor & Destructor](/php/bab-7-php-oop-dasar/7.3-constructor-and-destructor.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.2-property-and-method.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.
