├── Constructor&Destructor
├── orang.php
├── orang1.php
├── orang2.php
├── orang3.php
└── orang4.php
├── Ikantin
├── fungsi.php
└── index.php
├── Inheritance
├── imherit2.php
└── inherit1.php
├── Kalkulator
├── fungsi.php
└── index.php
├── Polymorphism
├── abstract.php
├── clss-abstract.php
├── in&ab.php
└── interface.php
├── README.md
├── Setter Getter
└── setget.php
├── Visibility
├── Visibility.php
└── fruit.php
├── konsep
├── bil.php
├── method-parameter.php
└── method.php
└── tugas kelompok
├── index.php
└── percobaan.php
/Constructor&Destructor/orang.php:
--------------------------------------------------------------------------------
1 | nama";
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/Constructor&Destructor/orang1.php:
--------------------------------------------------------------------------------
1 | nama="Rhoma Irama";
5 | $dhika->UcapSalam();
6 | echo "
";
7 | $shelly=new Orang(); // instansiasi
8 | $shelly->nama="Iwan Fals";
9 | $shelly->UcapSalam();
--------------------------------------------------------------------------------
/Constructor&Destructor/orang2.php:
--------------------------------------------------------------------------------
1 | nama=$nama;
6 | echo "Contructor: $this->nama dilahirkan
";
7 | }
8 | function UcapSalam(){
9 | echo "Hallo. Nama Saya adalah ".$this->nama."
";
10 | }
11 | function __destruct(){ //pembuatan destructor
12 | echo "Destructor: $this->nama meninggal dunia
";
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Constructor&Destructor/orang3.php:
--------------------------------------------------------------------------------
1 | UcapSalam();
5 | $orang2=new Orang("Orang 2");
6 | $orang2->UcapSalam();
--------------------------------------------------------------------------------
/Constructor&Destructor/orang4.php:
--------------------------------------------------------------------------------
1 | name dan saya berasal dari $this->address";
10 | }
11 |
12 | // destructor
13 | public function __destruct()
14 | {
15 | echo ' Ini dari destruktor.';
16 | }
17 |
18 | // constructor
19 | public function __construct()
20 | {
21 | echo 'Ini dari konstruktor. ';
22 | }
23 |
24 | }
25 |
26 | // membuat object $eliff
27 | $eliff = new User();
28 |
29 | // panggil method dari object $eliff
30 | echo $eliff->showBio();
31 | ?>
--------------------------------------------------------------------------------
/Ikantin/fungsi.php:
--------------------------------------------------------------------------------
1 | hargabakwan = 2000;
16 | $this->hargabakso = 10000;
17 | }
18 | }
19 |
20 | class Jumlah extends Produk {
21 | public function getJumlah($jmlbakwan, $jmlbakso){
22 | $this->jmlbakwan = $jmlbakwan;
23 | $this->jmlbakso = $jmlbakso;
24 | }
25 |
26 | public function setHarga() {
27 | $this->totalhargabakwan = $this->hargabakwan * $this->jmlbakwan;
28 | $this->totalhargabakso = $this->hargabakso * $this->jmlbakso;
29 | $this->totalseluruh = $this->totalhargabakwan + $this->totalhargabakso;
30 | }
31 |
32 | public function cetakStruk(){
33 | echo "******* Kantin Wikrama *******\n";
34 | echo "
";
35 | echo "Jumlah Bakwan: " . $this->jmlbakwan . "\n";
36 | echo "
";
37 | echo "Jumlah Bakso: " . $this->jmlbakso . "\n";
38 | echo "
";
39 | echo "Total Harga Bakwan: " . $this->totalhargabakwan . "\n";
40 | echo "
";
41 | echo "Total Harga Bakso: " . $this->totalhargabakso . "\n";
42 | echo "
";
43 | echo "Total Seluruh: " . $this->totalseluruh . "\n";
44 | echo "
";
45 | echo "*********************\n";
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Ikantin/index.php:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
10 |
11 |
12 |
86 |
113 |
114 |
115 |
116 |
117 |
118 |
159 |
160 | >
--------------------------------------------------------------------------------
/Inheritance/imherit2.php:
--------------------------------------------------------------------------------
1 | nama."
";
12 | }
13 | function UcapNegara(){ // penambahan method baru
14 | echo "I'm from ".$this->asal."
";
15 | }
16 | }
17 | $kabayan=new OrangSunda("Kabayan");
18 | $kabayan->UcapSalam();
19 | $william=new OrangInggris("Prince William");
20 | $william->UcapSalam();
21 | $william->UcapNegara();
--------------------------------------------------------------------------------
/Inheritance/inherit1.php:
--------------------------------------------------------------------------------
1 | nama=$nama;
6 | }
7 | function UcapSalam(){
8 | echo "Hallo. Nama Saya adalah ".$this->nama."
";
9 | }
10 | }
--------------------------------------------------------------------------------
/Kalkulator/fungsi.php:
--------------------------------------------------------------------------------
1 | x = $x;
8 | $this->x = $x;
9 | $hasil = $this-> x + $this->y;
10 | return $hasil;
11 |
12 | }
13 | public function setKali ($x, $y){
14 | $this->x = $x;
15 | $this->x = $x;
16 | $hasil = $this->x * $this->y;
17 | return $hasil;
18 |
19 | }
20 | public function setKurang ($x, $y){
21 | $this->x = $x;
22 | $this->x = $x;
23 | $hasil = $this->x - $this->y;
24 | return $hasil;
25 |
26 | }
27 | public function setBagi ($x, $y){
28 | $this->x = $x;
29 | $this->x = $x;
30 | $hasil = $this->x / $this->y;
31 | return $hasil;
32 |
33 | }
34 |
35 | }
36 | ?>
--------------------------------------------------------------------------------
/Kalkulator/index.php:
--------------------------------------------------------------------------------
1 | Latihan membuat program dengan OOP PHP';
6 | echo '
';
17 |
18 | if ($_POST) {
19 | $x = $_POST['x'];
20 | $y = $_POST['y'];
21 | if ($_POST['operasi'] == "tambah") {
22 | echo "Hasil: ".$x." + ".$y." = ".$kalku->setTambah($x, $y)."
";
23 | }
24 | elseif ($_POST['operasi'] == "kali") {
25 | echo "Hasil: ".$x." x ".$y." = ".$kalku->setKali($x, $y)."
";
26 | }
27 | elseif ($_POST['operasi'] == "kurang") {
28 | echo "Hasil: ".$x." - ".$y." = ".$kalku->setKurang($x, $y)."
";
29 | }
30 | elseif ($_POST['operasi'] == "bagi") {
31 | echo "Hasil: ".$x." / ".$y." = ".$kalku->setBagi($x, $y)."
";
32 | }
33 | }
34 | ?>
35 |
--------------------------------------------------------------------------------
/Polymorphism/abstract.php:
--------------------------------------------------------------------------------
1 | ";
11 | }
12 |
13 | public function Bergerak() {
14 | return "Burung bergerak dengan berjalan, terbang dan melompat
";
15 | }
16 |
17 | public function Beranak() {
18 | return "Burung beranak dengan bertelur
";
19 | }
20 | }
21 |
22 | class Kambing extends Hewan {
23 | public function Makan() {
24 | return "Kambing makan rumput
";
25 | }
26 |
27 | public function Bergerak() {
28 | return "Kambing bergerak dengan berjalan dan berlari
";
29 | }
30 |
31 | public function Beranak() {
32 | return "Kambing beranak dengan melahirkan
";
33 | }
34 | }
35 |
36 | $burung = new Burung;
37 | $kambing = new Kambing;
38 |
39 | echo "
Perilaku Burung : ";
40 | echo $burung->Makan();
41 | echo $burung->Bergerak();
42 | echo $burung->Beranak();
43 |
44 | echo "
";
45 | echo "
Perilaku Kambing : ";
46 | echo $kambing->Makan();
47 | echo $kambing->Bergerak();
48 | echo $kambing->Beranak();
--------------------------------------------------------------------------------
/Polymorphism/clss-abstract.php:
--------------------------------------------------------------------------------
1 | ";
12 | echo "My name is " . $this->showName();
13 | }
14 | }
15 |
16 | class Admin extends User
17 | {
18 | public function showName(){
19 | return "JM Admin";
20 | }
21 |
22 | public function showGreeting($greeting, $address = 'Banjar'){
23 | return $greeting . ", my name is " . $this->showName() . " from " . $address;
24 | }
25 | }
26 |
27 | class Editor extends User
28 | {
29 | public function showName(){
30 | return "Acep Editor";
31 | }
32 |
33 | public function showGreeting($greeting, $address = 'Banjar'){
34 | return $greeting . ", my name is " . $this->showName() . " from " . $address;
35 | }
36 |
37 | // bukan berasal dari abstract method
38 | public function showBio(){
39 | echo "This is Bio from " . $this->showName();
40 | }
41 | }
42 |
43 | class Reporter extends User
44 | {
45 | public function showName(){
46 | return "Novica Reporter";
47 | }
48 |
49 | public function showGreeting($greeting, $address = 'Banjar'){
50 | return $greeting . ", my name is " . $this->showName() . " from " . $address;
51 | }
52 | }
53 |
54 | $admin = new Admin;
55 | $editor = new Editor;
56 | $reporter = new Reporter;
57 |
58 | echo $admin->showGreeting("Good morning") . "
";
59 | echo $admin->showBio() . "
";
60 |
61 | echo $editor->showGreeting("Good night", "Jayapura") . "
";
62 | echo $editor->showBio() . "
";
63 |
64 | echo $reporter->showGreeting("Good evening", "Maluku") . "
";
65 | echo $reporter->showBio();
66 | ?>
--------------------------------------------------------------------------------
/Polymorphism/in&ab.php:
--------------------------------------------------------------------------------
1 | jariJari = $jariJari;
22 | }
23 |
24 | // menghitung luas tanah berbentuk lingkaran
25 | // implements method dari interface tanah
26 | public function hitungLuas() {
27 | return $this->jariJari * $this->jariJari * $this->pi;
28 | }
29 |
30 | // tanah ditanami Kopi
31 | // extneds method dari abstract class Bibit
32 | public function ditanami() {
33 | return "ditanami Kopi";
34 | }
35 | }
36 |
37 | class PersegiPanjang extends Bibit implements Tanah {
38 | private $panjang;
39 | private $lebar;
40 |
41 | public function __construct($panjang, $lebar) {
42 | $this->panjang = $panjang;
43 | $this->lebar = $lebar;
44 | }
45 |
46 | // menghitung luas tanah berbentuk persegi panjang
47 | // implements method dari interface tanah
48 | public function hitungLuas() {
49 | return $this->panjang * $this->lebar;
50 | }
51 |
52 | // tanah ditanami Padi
53 | // extneds method dari abstract class Bibit
54 | public function ditanami() {
55 | return "ditanami Padi";
56 | }
57 | }
58 |
59 | $tanahAgus = new Lingkaran(3);
60 | $tanahChandra = new PersegiPanjang(3,4);
61 |
62 | echo "Tanah Agus seluas " . $tanahAgus->hitungLuas() . " m2 \n";
63 | echo $tanahAgus->ditanami() . "\n \n";
64 |
65 | echo "Tanah Chandra seluas " . $tanahChandra->hitungLuas() . " m2 \n";
66 | echo $tanahChandra->ditanami() . "\n";
--------------------------------------------------------------------------------
/Polymorphism/interface.php:
--------------------------------------------------------------------------------
1 | ";
11 | }
12 |
13 | public function Bergerak() {
14 | return "Burung bergerak dengan berjalan, terbang dan melompat
";
15 | }
16 |
17 | public function Beranak() {
18 | return "Burung beranak dengan bertelur
";
19 | }
20 | }
21 |
22 | class Kambing Implements Hewan {
23 | public function Makan() {
24 | return "Kambing makan rumput
";
25 | }
26 |
27 | public function Bergerak() {
28 | return "Kambing bergerak dengan berjalan dan berlari
";
29 | }
30 |
31 | public function Beranak() {
32 | return "Kambing beranak dengan melahirkan
";
33 | }
34 | }
35 |
36 | $burung = new Burung;
37 | $kambing = new Kambing;
38 |
39 | echo "
Perilaku Burung : ";
40 | echo $burung->Makan();
41 | echo $burung->Bergerak();
42 | echo $burung->Beranak();
43 |
44 | echo "
";
45 | echo "
Perilaku Kambing : ";
46 | echo $kambing->Makan();
47 | echo $kambing->Bergerak();
48 | echo $kambing->Beranak();
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## LearningOOP
2 |
3 | # Bug
4 | Galat atau error yang dapat membuat aplikasi kita tidak dapat berjalan seperti keinginan.
5 |
6 | # Control Flow
7 | Dalam bahasa Indonesia dapat diartikan sebagai aliran kendali, artinya menjelaskan bagaimana urutan eksekusi perintah di dalam program.
8 | 2247
9 | # Debug
10 | Proses mencari dan menghilangkan galat atau error di dalam aplikasi untuk kemudian memperbaikinya.
11 |
12 | # Develop
13 | Proses penyusunan baris per baris kode sampai menjadi satu aplikasi utuh yang memiliki suatu fungsi.
14 |
15 | # Developer
16 | Seseorang yang bertugas membangun atau mengembangkan sebuah sistem.
17 |
18 | # Dokumentasi
19 | Kumpulan informasi tentang cara penggunaan tools atau komponen, serta informasi terkait
20 |
21 | # IDE
22 | Sebuah tools membantu tak hanya dalam proses developing, bahkan hingga optimasinya.
23 |
24 | OOP atau Object-Oriented Programming adalah suatu paradigma dalam pemrograman yang berbasis pada obyek
25 |
26 | # Programmer
27 | Seseorang yang menuliskan kode untuk menyusun sebuah program.
28 |
29 | # Tipe Data
30 | Digunakan untuk menentukan jenis nilai yang akan ditampung pada variabel.
31 |
32 | # Variable
33 | Tempat untuk menyimpan data sementara (di memori).
34 |
--------------------------------------------------------------------------------
/Setter Getter/setget.php:
--------------------------------------------------------------------------------
1 | =0))
6 | $this->tugas=$nilai;
7 | }
8 | function setuts($nilai){
9 | if(($nilai<=100)&&($nilai>=0))
10 | $this->uts=$nilai;
11 | }
12 | function setuas($nilai){
13 | if(($nilai<=100)&&($nilai>=0))
14 | $this->uas=$nilai;
15 | }
16 | function gettugas(){
17 | return $this->tugas;
18 | }
19 | function getuts(){
20 | return $this->uts;
21 | }
22 | function getuas(){
23 | return $this->uas;
24 | }
25 | function getNA(){
26 | $nilaiakhir=0.2*$this->tugas+0.3*$this->uts+0.5*$this->uas;
27 | return $nilaiakhir;
28 | }
29 | function tampil(){
30 | echo "Nilai Tugas : ".$this->tugas." Nilai UTS : ".$this->uts.
31 | " Nilai UAS : ".$this->uas." Nilai akhir : ".$this->getNA()."
";
32 | }
33 | } // End of Class Nilai
34 |
35 | // Contoh penggunaan class Nilai
36 | $nilai=new Nilai();
37 | $nilai->settugas(80); // Nilai berubah
38 | echo "Nilai Tugas sekarang adalah : ".$nilai->gettugas()."
";
39 | $nilai->setuts(60); // Nilai berubah
40 | $nilai->setuas(90); // Nilai berubah
41 | $nilai->tampil();
42 | $nilai->setuas(110); // Nilai uas tidak berubah, Nilai baru tidak valid
43 | $nilai->tampil();
44 | echo "Nilai akhir adalah : ".$nilai->getNA();
45 | ?>
--------------------------------------------------------------------------------
/Visibility/Visibility.php:
--------------------------------------------------------------------------------
1 | ";
8 | echo "Public : ".$this->public."
";
9 | echo "Protected : ".$this->protected."
";
10 | echo "Private : ".$this->private."
";
11 | }
12 | }
13 | $obj = new Visibility();
14 | $obj->CetakProperti();
15 | echo "
Pengaksesan dari luar class
";
16 | echo "Public : ".$obj->public."
"; // OK
17 | echo "Protected : ".$obj->protected."
"; // ERROR
18 | echo "Private : ".$obj->private."
"; // ERROR
--------------------------------------------------------------------------------
/Visibility/fruit.php:
--------------------------------------------------------------------------------
1 | name = $n;
9 | }
10 | protected function set_color($n) { // a protected function
11 | $this->color = $n;
12 | }
13 | private function set_weight($n) { // a private function
14 | $this->weight = $n;
15 | }
16 | }
17 |
18 | $mango = new Fruit();
19 | $mango->set_name('Mango'); // OK
20 | $mango->set_color('Yellow'); // ERROR
21 | $mango->set_weight('300'); // ERROR
22 | ?>
--------------------------------------------------------------------------------
/konsep/bil.php:
--------------------------------------------------------------------------------
1 | merk = $merk;
11 | $this->nama = $nama;
12 | $this->warna = $warna;
13 | $this->kecepatan = $kecepatan;
14 | }
15 | public function getKondisiMobil() {
16 | echo "Kondisi mobil ";
17 | if ($this->kecepatan > 0) {
18 | echo "maju";
19 | }
20 | else {
21 | echo "mundur";
22 | }
23 | }
24 |
25 | public function getCetak() {
26 | echo "
Merk Mobil: ".$this->merk;
27 | echo "
Nama Mobil: ".$this->nama;
28 | echo "
Warna Mobil: ".$this->warna;
29 | echo "
Kecepatan Mobil: ".$this->kecepatan;
30 | }
31 |
32 | }
33 | $mobil1 = new Mobil();
34 | $mobil1->inputDataMobil("Toyota", "Fortuner", "Hitam", 250);
35 | $mobil1->getKondisiMobil("");
36 | $mobil1->getCetak();
37 | echo "
";
38 |
39 | $mobil2 = new Mobil();
40 | $mobil2->inputDataMobil("Honda", "Brio", "Hijau", -100);
41 | $mobil2->getKondisiMobil("
Hijau
");
42 | // $mobil2->inputDataMobil("Honda", "Brio", "
Hijau
", -100);
43 | $mobil2->getCetak();
44 |
45 | ?>
--------------------------------------------------------------------------------
/konsep/method-parameter.php:
--------------------------------------------------------------------------------
1 |
10 |
11 |
Saya Elif, Saya membawa('kanan') ?>.
12 |
Namun, terkadang saya juga membawa('kiri') ?>.
--------------------------------------------------------------------------------
/konsep/method.php:
--------------------------------------------------------------------------------
1 |
11 |
12 |
Saya Eliff, Saya dapat berjalan() ?>.
--------------------------------------------------------------------------------
/tugas kelompok/index.php:
--------------------------------------------------------------------------------
1 | nilai = $nilai;
7 | }
8 |
9 | public function SetNilai($nilai) {
10 | $this->nilai = $nilai;
11 | }
12 |
13 | public function GetNilai() {
14 | $angka = $this->nilai;
15 |
16 | if ($this->nilai >= 10 && $this->nilai < 75) {
17 | return "Tidak Kompeten";
18 | } else if ($this->nilai >= 75 && $this->nilai < 79) {
19 | return "Pas KKM";
20 | } else if ($this->nilai == 80) {
21 | return "bagus";
22 | }else {
23 | return"sangat bagus";
24 | }
25 |
26 | return $angka;
27 | }
28 | }
29 |
30 | $nilait = 60;
31 | $nilaiATS = 75;
32 | $nilaiUTS = 80;
33 |
34 | $nilai = new Nilai();
35 | echo "Nilai Tugas:$nilait";
36 | echo "
";
37 | echo "Nilai ATS:$nilaiATS";
38 | echo "
";
39 | echo "Nilai UTS:$nilaiUTS";
40 | echo "
";
41 | echo "
";
42 | $nilai->SetNilai($nilait);
43 | echo "Nilai Tugas Anda: " . $nilai->GetNilai();
44 | echo "
";
45 | echo "
";
46 | $nilai->SetNilai($nilaiATS);
47 | echo "Nilai ATS Anda: " . $nilai->GetNilai();
48 | echo "
";
49 | echo "
";
50 | $nilai->SetNilai($nilaiUTS);
51 | echo "Nilai UTS Anda: " . $nilai->GetNilai();
52 | echo "
";
53 | echo "
";
54 | echo "Nilai Tertinggi Anda: " . max($nilait, $nilaiATS, $nilaiUTS);
55 | echo "
";
56 | echo "Nilai Terendah Anda: " . min($nilait, $nilaiATS, $nilaiUTS);
57 | echo "
";
58 | echo "
";
59 |
60 | $rata_rata = ($nilait + $nilaiATS + $nilaiUTS) / 3;
61 |
62 | echo "Nilai Rata-Rata Anda = ($nilait + $nilaiATS + $nilaiUTS) / 3 = " . $rata_rata;
63 |
64 |
65 |
66 | ?>
--------------------------------------------------------------------------------
/tugas kelompok/percobaan.php:
--------------------------------------------------------------------------------
1 | nilai = $nilai;
7 | }
8 |
9 | public function SetNilai($nilai) {
10 | $this->nilai = $nilai;
11 | }
12 |
13 | public function GetNilai() {
14 | $angka = $this->nilai;
15 |
16 | if ($this->nilai >= 10 && $this->nilai < 75) {
17 | return "Tidak Kompeten";
18 | } else if ($this->nilai >= 75 && $this->nilai < 79) {
19 | return "Pas KKM";
20 | } else if ($this->nilai == 80) {
21 | return "bagus";
22 | }else {
23 | return"sangat bagus";
24 | }
25 |
26 | return $angka;
27 | }
28 | }
29 |
30 | $nilait = 60;
31 | $nilaiATS = 75;
32 | $nilaiUTS = 80;
33 |
34 | $nilai = new Nilai();
35 | echo "Nilai Tugas:$nilait";
36 | echo "
";
37 | echo "Nilai ATS:$nilaiATS";
38 | echo "
";
39 | echo "Nilai UTS:$nilaiUTS";
40 | echo "
";
41 | echo "
";
42 | $nilai->SetNilai($nilait);
43 | echo "Nilai Tugas Anda: " . $nilai->GetNilai();
44 | echo "
";
45 | echo "
";
46 | $nilai->SetNilai($nilaiATS);
47 | echo "Nilai ATS Anda: " . $nilai->GetNilai();
48 | echo "
";
49 | echo "
";
50 | $nilai->SetNilai($nilaiUTS);
51 | echo "Nilai UTS Anda: " . $nilai->GetNilai();
52 | echo "
";
53 | echo "
";
54 | echo "Nilai Tertinggi Anda: " . max($nilait, $nilaiATS, $nilaiUTS);
55 | echo "
";
56 | echo "Nilai Terendah Anda: " . min($nilait, $nilaiATS, $nilaiUTS);
57 | echo "
";
58 | echo "
";
59 |
60 | $rata_rata = ($nilait + $nilaiATS + $nilaiUTS) / 3;
61 |
62 | echo "Nilai Rata-Rata Anda = ($nilait + $nilaiATS + $nilaiUTS) / 3 = " . $rata_rata;
63 |
64 |
65 |
66 | ?>
--------------------------------------------------------------------------------