Contoh program objek berbasis PHP.

 berikut ini adalah contoh program objek berbasis PHP dengan metode Acces Modifier

<?php
/* accessmodifier.php */
class KendBermotor {
public $mesin;
private $roda;
protected $jalur;
function __construct() {
$this->mesin = "Kendaraan Bermotor Punya Mesin <br>";
$this->roda = "Kendaraan Bermotor Punya Roda <br>";
$this->jalur = "Kendaraan Bermotor Punya Jalur <br>";
}
function getMesin() {
return $this->mesin;
}
function getJalur() {
return $this->jalur;
}
function getRoda() {
return $this->roda;
}
}
//Class turunan dari KendBermotor
class KapalLaut extends KendBermotor {
private $baling2;
function __construct() {
// Dapat dijalankan
$this->mesin = "Kapal Laut Punya Mesin <br>";
$this->jalur = "Kapal Laut Punya Jalur <br>";
$this->baling2 = "Kapal Laut Punya Baling-baling <br>";
// Tidak dapat diakses
$this->roda = "Kapal Laut Punya Roda";
}
function getBaling2() {
return $this->baling2;
}
}
$KB = new KendBermotor();
echo "<p><b> Kriteria Kendaraan Bermotor : </b><br>";
echo $KB->getMesin();
echo $KB->getRoda();
echo $KB->getJalur();
$KL = new KapalLaut();
echo "<p><b> Kriteria Kapal Laut : </b><br>";
// Dapat diakses
echo $KL->getMesin();
echo $KL->getBaling2();
echo $KL->getJalur();
// Tidak dapat diakses
echo "Coba mengakses roda";
echo $PB->getRoda();
?>

berikut hasilnya..... chek it dot


Previous
Next Post »