Created
July 6, 2013 04:11
-
-
Save Imam86/5938613 to your computer and use it in GitHub Desktop.
Lesson File : "Mudahnya Belajar OOP di PHP - Part II" --- For Group : http://www.facebook.com/groups/belajar.ilmu.website/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Deklarasi class. | |
class mahasiswa_constructor { | |
// Deklarasi property. | |
public $nim; | |
public $nama_mhs; | |
public $output; | |
// Deklarasi constructor versi PHP 4, tidak akan dieksekusi oleh PHP versi jaman sekarang (versi 5). | |
public function mahasiswa_constructor ($x, $y) { | |
$this->nim = $x; // nilai dari property $nim akan mengikuti parameter $x. | |
$this->nama_mhs = $y; // nilai dari property $nama_mhs akan mengikuti parameter $y. | |
// nilai dari property $output, berisi nilai property $nim dan $nama_mhs. | |
$this->output = "Yang memiliki NIM = ".$this->nim." adalah ".$this->nama_mhs.".<br />"; | |
$this->panggil_mhs(); // method lain yang dipanggil secara otomatis oleh constructor. | |
} | |
// Deklarasi constructor versi PHP 5, menggunakan Magic Methods __construct. | |
public function __construct ($x, $y) { | |
$this->nim = $x; // nilai dari property $nim akan mengikuti parameter $x. | |
$this->nama_mhs = $y; // nilai dari property $nama_mhs akan mengikuti parameter $y. | |
// nilai dari property $output, berisi nilai property $nim dan $nama_mhs. | |
$this->output = "Mahasiswa dengan NIM = ".$this->nim." adalah ".$this->nama_mhs.".<br />"; | |
$this->panggil_mhs(); // method lain yang dipanggil secara otomatis oleh constructor. | |
} | |
// Deklarasi method. | |
public function panggil_mhs() { | |
// memanggil nilai dari property $output. | |
echo $this->output; | |
} | |
} // Tutup deklarasi class. | |
/* Melakukan proses pembuatan object baru, dan langsung diberikan | |
nilai di property melalui parameter pada constructor. */ | |
$imam = new mahasiswa_constructor('101051096', 'Imam Kurniawan'); | |
$hesty = new mahasiswa_constructor('101051084', 'Hesty Dwi Suryaningsih'); | |
/* Pada contoh pembuatan object $imam di atas, nilai pada parameter $x adalah '101051096', | |
dan nilai pada parameter $y adalah 'Imam Kurniawan'. | |
Begitu pula dengan pembuatan object $hesty, yaitu memberikan nilai pada parameter $x dan $y. | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment