<title>Parent - PHP OOP</title> <?php class komputer { public function spec() { // Ini Parent Method return "Spec Komputer : Acer, \n Processor Intel core i3, Ram 2GB"; } } class laptop extends komputer { public function spec() { // Ini Child Method return "Spec Laptop : Asus, \n Processor Intel core i5, Ram 4GB, VGA NVIDIA GeForce 820m"; } public function spec_komputer() { // Cara memanggil Parent Method return parent::spec(); } } $hardware = new laptop(); // Panggil Method spec() echo $hardware->spec(); echo "<br />"; // Panggil Method spec_komputer() echo $hardware->spec_komputer();
<?php // buat class komputer class komputer { // property dengan hak akses protected protected $jenis_processor = "Intel Core i7-4790 3.6Ghz"; } // buat class laptop class laptop extends komputer { public function tampilkan_processor() { return $this->jenis_processor; } } // buat objek dari class laptop (instansiasi) $laptop_baru = new laptop(); // jalankan method echo $laptop_baru->tampilkan_processor(); // "Intel Core i7-4790 3.6Ghz"
<?php // buat class laptop class laptop { // buat public property public $pemilik; // buat public method public function hidupkan_laptop() { return "Hidupkan Laptop"; } } // buat objek dari class laptop (instansiasi) $laptop_anto = new laptop(); // set property $laptop_anto->pemilik = "Anto"; // tampilkan property echo $laptop_anto->pemilik; // Anto // tampilkan method echo $laptop_anto->hidupkan_laptop(); // "Hidupkan Laptop"
// Ini abstract class public abstract function spesifikasi(); // Ini abstract method (Include) const PRICE = 20000000; public static function harga() { // Ini method biasa return self::PRICE; } } class laptop extends komputer { public function spesifikasi() { // Implementasi abstract method (Hilangkan) return "Lihat spesifikasi laptop"; } public function merek_laptop() { return "ASUS ROG"; } } // $abstrak = new komputer(); // Apakah bisa menginisiasi Abstract Class komputer? // echo komputer::harga(); // Apakah bisa menginisiasi Abstract Class komputer dengan static method? (Ganti Spesifikasi) $beli = new laptop(); echo $beli->spesifikasi(); // (Hilangkan) echo "<br />"; echo $beli->merek_laptop(); echo "<br />"; echo "Rp. " . $beli->harga();
<title>Final - PHP OOP</title> <?php class komputer { final function lihat_spec() { // final method return "Lihat Spesifikasi Komputer"; } } class laptop { public function lihat_spec() { return "Lihat Spesifikasi Laptop"; } } $com = new laptop(); echo $com->lihat_spec();
<?php // buat class laptop class laptop { // buat protected property protected $pemilik = "Anto"; public function akses_pemilik() { return $this->pemilik; } protected function hidupkan_laptop() { return "Hidupkan Laptop"; } public function paksa_hidup() { return $this->hidupkan_laptop(); } } // buat objek dari class laptop (instansiasi) $laptop_anto = new laptop(); // jalankan method akses_pemilik() echo $laptop_anto->akses_pemilik(); // "Anto" // jalankan method paksa_hidup() echo $laptop_anto->paksa_hidup(); // "Hidupkan Laptop"
<title>Constant - PHP OOP</title> <?php class laptop { const DOLLAR = 14000; public function beli_laptop($harga) { return "Beli Komputer Baru, Rp. " . $harga * self::DOLLAR; } } $laptop_baru = new laptop(); echo $laptop_baru->beli_laptop('500');