Home » PHP » OOP PHP : Inheritance / Warisan – Kode dan Contohnya

OOP PHP : Inheritance / Warisan – Kode dan Contohnya

by Hanifah Nurbaeti
by Hanifah Nurbaeti

Section Artikel

PHP – Apa itu Inheritance?

Pewarisan atau Inheritance dalam OOP = Ketika kelas diturunkan dari kelas lain.

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

Kelas yang diwariskan ditentukan dengan menggunakan kata kunci extends.

Mari kita lihat contohnya:

Contoh :

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

// Strawberry diwarisi dari Buah
class Strawberry extends Fruit {
  public function message() {
    echo "Apakah saya buah atau beri? ";
  }
}
$strawberry = new Strawberry("Strawberry", "merah");
$strawberry->message();
$strawberry->intro();
?>
// Output : Apakah saya buah atau beri? Buahnya Strawberry dan warnanya merah.

Penjelasan Kode :

  • Kelas Strawberry diwarisi dari kelas Buah.
  • Ini berarti bahwa kelas Strawberry dapat menggunakan properti $name dan $color serta metode public __construct() dan intro() dari kelas Fruit karena inheritance atau pewarisan.
  • Kelas Strawberry juga memiliki metodenya sendiri, yaitu : message().

PHP – Inheritance dan Protected Access Modifier

Sebelumnya kita telah mempelajari bahwa properti atau metode protected dapat diakses di dalam kelas dan oleh kelas yang diturunkan dari kelas itu. Apa artinya?

Mari kita lihat contohnya:

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 "Apakah saya buah atau beri? ";
  }
}

// Coba panggil ketiga metode dari luar kelas
$strawberry = new Strawberry("Strawberry", "red");  // OK. __construct() adalah public
$strawberry->message(); // OK. message() adalah public
$strawberry->intro(); // ERROR. intro() adalah protected
?>
  
  //Output : Apakah saya buah atau beri?

Dalam contoh di atas kita melihat bahwa jika kita mencoba memanggil metode protected (intro ()) dari luar kelas, kita akan menerima kesalahan. metode public 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 "Buahnya {$this->name} dan warnya {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public function message() {
    echo "Apakah saya buah atau beri? ";
    // Panggil metode yang dilindungi dari dalam kelas turunan - OK
    $this -> intro();
  }
}

$strawberry = new Strawberry("Strawberry", "merah"); // OK. __construct() is public
$strawberry->message(); // OK. message () bersifat publik dan memanggil intro () (yang dilindungi) dari dalam kelas turunan
?>
  //Output : Apakah saya buah atau beri? Buahnya Strawberry dan warnanya merah.

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

PHP – Mengganti Metode Inheritance

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

Lihat contoh di bawah ini. Metode __construct() dan intro() di kelas anak (Strawberry) akan menimpa 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 "Buahnya {$this->name} dan warnanya {$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 "Buahnya {$this->name}, warnanya {$this->color}, dan beratnya {$this->weight} gram.";
  }
}

$strawberry = new Strawberry("Strawberry", "merah", 50);
$strawberry->intro();
?>
  
  //Output : Buahnya Strawberry, warnanya merah, berat 50 gram.

PHP – Kata Kunci final

Kata kunci final dapat digunakan untuk mencegah pewarisan kelas atau untuk mencegah penimpaan 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
}
?>
  
// output : PHP Fatal error: Class Strawberry may not inherit from final class (Fruit) in /home/o6MTL0/prog.php on line 10

Contoh berikut menunjukkan cara mencegah penimpaan metode:

Contoh :

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

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

  //Output : PHP Fatal error: Cannot override final method Fruit::intro() in /home/sdpysj/prog.php on line 14

You may also like