Anda di halaman 1dari 24

Pemrograman Web II

Team Dosen :
Agus Ilyas, S.Kom.
Anas Syaifudin, S.Kom.
Arocham, M.Kom.
Christian Y. R., M.Kom.
Esti Mulyani, S.Kom.
M. Faizal Kurniawan, S.Kom

Web Programming with Framework

Materi
No

Materi

Pert.

Object Oriented Programming in PHP

Framework CodeIgniter Introduction

MVC Concept

Helper and Library

Database with CodeIgniter

Project with CRUD and Template

Jumlah Pertemuan

14

Software
Editor
Netbeans + PHP Plugins
https://netbeans.org/downloads/

Webserver, MySQL
XAMPP bundle
https://www.apachefriends.org/download.html

Framework
CodeIgniter http://www.codeigniter.com/download

Kontrak Kuliah
No

Komponen

Persentase

Presensi

10%

UTS

30%

UAS

30%

Tugas

30%

Tugas wajib diupload

Tugas wajib dikumpulkan secara kolektif perkelas oleh ketua kelas

Object Oriented Programming

Object Oriented Programming

OOP is that it presents not just new syntax but a new way of thinking
about a problem

Pendekatan berdasarkan object

Setiap bagian dari program object

2 hal yang penting didalam OOP :

Class

Object

Class and Object

Class

A class is a generalized definition of a thing

Think of classes as blueprints

Object

An object is a specific implementation of that thing

Think of objects as the house built using the blueprint as a guide.

Class

Class adalah sebuah cetakan/blueprint dari object

Class merupakan gambaran/definisi dari sesuatu

Informasi apa saja yang dimiliki (variable/attributes/properties)

Fungsi yang dimungkinkan dengan data/informasi yang dimiliki


(function/methods)

Pendefinisian class dimulai dengan keyword class

Diikuti oleh nama class, ditulis dengan gaya huruf besar

Definisi class diletakan didalam tanda kurung kurawal

Sintak
class ClassName{
access_modifier $var;
function functionName(){
// function code

}
}

Contoh
<?php
class Customer{
public $customer_id;
protected $customer_name;
protected $customer_address;

function set_customer_id($cust_id){
$this->customer_id = $cust_id;
}
function set_customer_name($cust_name){
$this->customer_name = $cust_name;
}
function get_customer_name(){
return $this->customer_name;
}
function display_customer(){
echo "Customer ID : ",$this->customer_id,"<br />";
echo "Customer Name : ",$this->customer_name,"<br />";
}
}
?>

Object

Object adalah implementasi dari Class

Object merupakan hasil instansiasi dari Class

Resource yang ada pada class object

Cara membuat Object


<?php
// file yang memuat class
require_once('class_customer.php');
//proses instansiasi object
$andi = new Customer();
$budi = new Customer();
?>
The include and require statements are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stop the script
include will only produce a warning (E_WARNING) and the script will
continue

Mengakses Resource

Class Object, Object Class

<?php
error_reporting(E_ALL);
require "customer.php";
$cust = new Customer();
$cust->set_customer_id("CI-150913001");
$cust->set_customer_name("STMIK Widya Pratama");
$cust->display_customer();
echo $cust->customer_id,"<br />";
echo $cust->get_customer_name();
?>

Modifiers

Pada contoh kita sudah menggunakan modifier private dan public

encapsulasi/encapsulation

Terdapat 3 modifier dalam PHP :

private hanya bisa diakses pada class itu sendiri

Protected bisa diakses oleh subclass-nya

Public bisa diakses oleh seluruh class

Constructor dan Destructor

Constructor merupakan function yang akan dieksekusi pada waktu class


tersebut diinstansiasi

Pembuatan constructor :

function __construct([params]){
statements;
}

Destructor merupakan function yang akan dieksekusi pada waktu class


tersebut selesai digunakan/akhir script

Pembuatan constructor :

function __destruct([params]){
statements;

Contoh constructor
<?php
class Customer{
public $customer_id;
private $customer_name;
private $customer_address;

function __construct(){
echo "constructor dijalankan <br />";
}
function set_customer_id($cust_id){
$this->customer_id = $cust_id;
}
}
?>

Contoh Destructor
<?php
class Customer{
public $customer_id;
private $customer_name;
private $customer_address;

function __construct(){
echo "constructor dijalankan <br />";
}
function __destruct(){
echo "<br />destructor dijalankan <br />";
}

}
?>

Pewarisan/Inheritance

Class dapat mewariskan apa yang dimilikinya kepada class yang lain

Class yang mewarisi superclass (parent)

Class yang diwarisi subclass (children)

Pewarisan menggunakn keyword extends

Contoh :

class Member extends Customer{


}

Contoh Pewarisan
<?php
require_once "customer.php";
class Member extends Customer{
public $member_id;
function get_member_id(){
return $this->member_id;
}

function display_member(){
echo "<br />";
echo "=====================================<br />";
$this->display_customer();
echo "=====================================<br />";
}
}
?>

Contoh Pewarisan
<?php
require "customer.php";
require "member.php";

$cust = new Customer();


$cust->set_customer_id("CI-150913001");
$cust->set_customer_name("STMIK Widya Pratama");
$cust->display_customer();
echo $cust->customer_id,"<br />";
echo $cust->get_customer_name();
echo "<br />";
$member = new Member();
$member->set_customer_id("MI-150914001");
$member->set_customer_name("Widya Pratama");
$member->display_member();
?>

Contoh Pewarisan

Overriding

Kemampuan subclass memiliki nama method/function yang sama


dengan superclass-nya

Jika function dipanggil dari object, maka function pada subclass yang
akan dikerjakan/dieksekusi

<?php
require_once "customer.php";
class Member extends Customer{
public $member_id;
function set_member_id($id){
$this->member_id = $id;
}
function get_member_id(){
return $this->member_id;
}
. . .
function display_customer(){
echo "<br />";
echo "======================================<br />";
echo "Member ID : ",$this->member_id,"<br />";
echo "Customer ID : ",$this->customer_id,"<br />";
echo "Customer Name : ",$this->customer_name,"<br />";
echo "======================================<br />";
}

}
?>

Hasil