Tutorial PHP

RUMAH PHP Pengenalan PHP Instal PHP Sintaks PHP Komentar PHP Variabel PHP PHP Gema / Cetak Tipe Data PHP String PHP Nomor PHP PHP Matematika Konstanta PHP Operator PHP PHP Jika...Lain...Elseif Beralih PHP PHP Loop Fungsi PHP Array PHP PHP Superglobal PHP RegEx

Formulir PHP

Penanganan Formulir PHP Validasi Formulir PHP Diperlukan Formulir PHP URL/Email Formulir PHP Formulir PHP Lengkap

PHP Lanjutan

Tanggal dan Waktu PHP PHP Termasuk Penanganan File PHP File PHP Buka/Baca Membuat/Menulis File PHP Unggah File PHP Cookie PHP Sesi PHP Filter PHP Filter PHP Tingkat Lanjut Fungsi Panggilan Balik PHP PHP JSON Pengecualian PHP

PHP OOP

PHP Apa itu OOP Kelas/Objek PHP Konstruktor PHP Penghancur PHP Pengubah Akses PHP Warisan PHP Konstanta PHP Kelas Abstrak PHP Antarmuka PHP Ciri-ciri PHP Metode Statis PHP Properti Statis PHP Ruang Nama PHP PHP Iterable

Database MySQL

Database MySQL Koneksi MySQL MySQL Buat DB MySQL Buat Tabel MySQL Sisipkan Data MySQL Dapatkan ID Terakhir MySQL Sisipkan Beberapa MySQL Disiapkan MySQL Pilih Data MySQL Dimana MySQL Dipesan Oleh MySQL Hapus Data Data Pembaruan MySQL Data Batas MySQL

PHP XML

PHP XML Parser PHP SimpleXML Parser PHP SimpleXML - Dapatkan PHP XML Ekspat PHP XML DOM

PHP - AJAX

Pengenalan AJAX AJAX PHP Basis Data AJAX AJAX XML Pencarian Langsung AJAX Jajak Pendapat AJAX

Contoh PHP

Contoh PHP Kompilator PHP Kuis PHP Latihan PHP Sertifikat PHP

Referensi PHP

Ikhtisar PHP Array PHP Kalender PHP Tanggal PHP Direktori PHP Kesalahan PHP Pengecualian PHP Sistem File PHP Filter PHP PHP FTP PHP JSON Kata Kunci PHP PHP Libxml Surat PHP PHP Matematika PHP Lain-lain PHP MySQLi Jaringan PHP Kontrol Keluaran PHP PHP RegEx PHP SimpleXML Aliran PHP String PHP Penanganan Variabel PHP PHP XML Parser PHP Zip Zona Waktu PHP

PHP OOP - Warisan


PHP - Apa itu Warisan?

Warisan dalam OOP = Ketika sebuah kelas diturunkan dari kelas lain.

Kelas anak akan mewarisi semua properti dan metode publik dan yang dilindungi dari kelas induk. Selain itu, ia dapat memiliki properti dan metodenya sendiri.

Kelas yang diwarisi didefinisikan dengan menggunakan extends kata kunci.

Mari kita lihat sebuah contoh:

Contoh

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
  }
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>

Contoh Dijelaskan

Kelas Strawberry diwarisi dari kelas Buah.

Ini berarti bahwa kelas Strawberry dapat menggunakan properti $name dan $color publik serta metode __construct() dan intro() publik dari kelas Fruit karena pewarisan.

Kelas Strawberry juga memiliki metodenya sendiri: message().



PHP - Warisan dan Pengubah Akses yang Dilindungi

Pada bab sebelumnya kita telah mempelajari bahwa protectedproperti atau metode dapat diakses di dalam kelas dan oleh kelas yang diturunkan dari kelas tersebut. Apa artinya?

Mari kita lihat sebuah contoh:

Contoh

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
  }
}

// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red");  // OK. __construct() is public
$strawberry->message(); // OK. message() is public
$strawberry->intro(); // ERROR. intro() is protected
?>

Pada contoh di atas kita melihat bahwa jika kita mencoba memanggil protected metode (intro()) dari luar kelas, kita akan menerima kesalahan. public metode akan bekerja dengan baik!

Mari kita lihat contoh lain:

Contoh

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
    // Call protected method from within derived class - OK
    $this -> intro();
  }
}

$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class
?>

Dalam contoh di atas kita melihat bahwa semuanya bekerja dengan baik! Itu karena kita memanggil protected metode (intro()) dari dalam kelas turunan.


PHP - Mengganti Metode yang Diwarisi

Metode yang diwariskan dapat diganti dengan mendefinisikan ulang metode (menggunakan nama yang sama) di kelas anak.

Lihatlah contoh di bawah ini. Metode __construct() dan intro() di kelas anak (Strawberry) akan menggantikan metode __construct() dan intro() di kelas induk (Buah):

Contoh

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public $weight;
  public function __construct($name, $color, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->weight = $weight;
  }
  public function intro() {
    echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
  }
}

$strawberry = new Strawberry("Strawberry", "red", 50);
$strawberry->intro();
?>

PHP - Kata Kunci terakhir

Kata final kunci dapat digunakan untuk mencegah pewarisan kelas atau untuk mencegah penggantian metode.

Contoh berikut menunjukkan cara mencegah pewarisan kelas:

Contoh

<?php
final class Fruit {
  // some code
}

// will result in error
class Strawberry extends Fruit {
  // some code
}
?>

Contoh berikut menunjukkan cara mencegah penggantian metode:

Contoh

<?php
class Fruit {
  final public function intro() {
    // some code
  }
}

class Strawberry extends Fruit {
  // will result in error
  public function intro() {
    // some code
  }
}
?>