Konsep OOP

Script myClassKaruniaSuciLestari.php :

<?php
class Mahasiswa
{
// Property
public $nim;
public $nama;
public $nilai;

// Method
function StatusNilai()
{
if ($this->nilai >= 70) {
$status = “Lulus”;
} else {
$status = “Gagal”;
}
return $status;
}
}

// Instantisasi Objek
$mahasiswa = new Mahasiswa();

// Memberikan Nilai Pada Property
$mahasiswa->nim = 1414382205;
$mahasiswa->nama = “Karunia Suci Lestari”;
$mahasiswa->nilai = 90;

// Mencetak Hasil
echo “NIM : “.$mahasiswa->nim.”<br />”;
echo “Nama : “.$mahasiswa->nama.”<br />”;
echo “Nilai : “.$mahasiswa->nilai.”<br />”;
echo “Status : “.$mahasiswa->StatusNilai().”<br /><br />”;
?>
PrintScreen myClassKaruniaSuciLestari.php :
Script untuk newStudent.php :

<?php
/*
* Memberikan Nilai Property Dengan Method
*/
class Mahasiswa
{
public $nim;
public $nama;
public $nilai;

function StatusNilai()
{
if ($this->nilai >= 70) {
$status = “Lulus”;
} else {
$status = “Gagal”;
}
return $status;
}

function SetNim($nim)
{
$this->nim = $nim;
}

function SetNama($nama)
{
$this->nama = $nama;
}

function SetNilai($nilai)
{
$this->nilai = $nilai;
}

function BacaNim()
{
echo $this->nim;
}

function BacaNama()
{
echo $this->nama;
}

function BacaNilai()
{
echo $this->nilai;
}
}

$mahasiswabaru = new Mahasiswa();
$mahasiswabaru->SetNim(1414382205);
$mahasiswabaru->SetNama(‘Karunia Suci Lestari’);
$mahasiswabaru->SetNilai(65);
$mahasiswabaru->StatusNilai();

echo “Nim : “;
$mahasiswabaru->BacaNim();
echo “<br />”;
echo “Nama : “;
$mahasiswabaru->BacaNama();
echo “<br />”;
echo “Nilai : “;
$mahasiswabaru->BacaNilai();
echo “<br />”;
echo “Status : “;
echo $mahasiswabaru->StatusNilai();
?>

PrintScreen newStudent.php :

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s