7. This Keyword
This Keyword :
- Saat Kita Membuat Kode Di Dalam function Di Dalam class, Kita Bisa Menggunakan Kata Kunci this
Untuk Mengakses Object Saat Ini
- Misal Kadang Kita Butuh Mengakses Properties Atau function Lain Di classs Yang Sama
Contoh Kodingannya :
thiskeyword-7.php
class SayKeyword {
function ThisKeyword (?string $name) {
if (is_null($name)) {
// Cara 1
// echo "Alhamdulillah Baik Bro $this->name" . PHP_EOL;
// Cara 2
echo "Hai Bro {$this->name}" . PHP_EOL;
}
else {
echo "Baik Bro $name, Ente Gimana Bro {$this->name}" . PHP_EOL;
}
}
}
object.php
require_once "thiskeyword-7.php";
$thiskeyword = new SayKeyword();
$thiskeyword->name = "Eko";
$thiskeyword->ThisKeyword("Bambang");
$kosong = new SayKeyword();
$kosong->name = "Eko";
$kosong->ThisKeyword(null);

Belum ada Komentar untuk "7. This Keyword"
Posting Komentar