Anda di halaman 1dari 31

Web Programming

Object Oriented Programming and MVC

§ Aryo Pinandito, ST, M.MT


What is a function?
§ Conceptually, what does a function represent?

Action or Method
…give the function something
(arguments), it does something with
them, and then returns a result…
What is a class?
§ Conceptually, a class represents an object, with
associated methods (behaviours) and variables
(attributes)
Class Definition Example
Put the class definition into .php file, in this example in:
manusia.class.php

<?php
class manusia {
public $nama;
public function sayHello() {
echo 'Halo!';
}
}
?>
Class Defintion
§ Similar to defining a function..
§ The definition does not do anything by itself. It is
a blueprint, or description, of an object. To do
something, you need to use the class…
§ It means instantiating the class into object
Instantiating Class Into Object
§ Instantiate class into object without calling its
constructor function:
§ $objectVar = new ClassName;

§ Instantiate class into object and call its


constructor
§ $objectVar = new ClassName();
Class Usage
Before instantiating the class PHP need to know its definition,
so "include it only once" the class file definition beforehand
<?php
require_once('manusia.class.php'); Includes class
definition
$susan = new manusia;
$susan->nama = 'Susan'; Attribute access,
echo $susan->nama; set and get

echo ' bilang ';


echo $susan->sayHello(); Method access
?>
Attributes From Within The
Class
class manusia {
public $nama;
public function sayHello() {
echo $this->nama . ' bilang Halo!';
}
} $this is a reference that
refers to its own class

$susan = new manusia;


$susan->nama = 'Susan';
$susan->sayHello();
Constructor methods
§ A constructor method is a function that is
automatically executed when the class is first
instantiated.
§ Create a constructor by including a function
within the class definition with the __construct()
name.
§ Remember, if the constructor requires
arguments, they must be passed when it is
instantiated!
Constructor Example
<?php
class manusia {
public $nama;
public function __construct($nama) {
$this->nama = $nama;
}
public function sayHello() {
echo $this->nama . ' bilang Halo!';
}
}

$susan = new manusia("Susan");


$susan->sayHello(); Output:
Susan bilang Halo!
Inheritance
§ The real power of using classes is the property of
inheritance – creating a hierarchy of interlinked
classes.

Manusia
superclass

subclass
Dosen Mahasiswa
Inheritance Manusia

§ The subclasses 'inherit' all


the superclass public, Mahasiswa
protected, and default
methods and variables,
§ Subclass can also have public class Mahasiswa
their own methods and extends Manusia {
attribute variables ...
§ Inheritance in PHP uses
}
extends keyword
Constructors in Inheritance
§ If subclass possesses a constructor, it is executed
when instantiated and its superclass constructor
is ignored.
§ If subclass does not have a constructor, its
superclass constructor will be executed.
Class Visibility
Members = Class' attributes and methods
§ Visibility
§ The visibility of a property or method prefixed with keywords
public, protected, or private.
§ Class members declared public can be accessed everywhere.
Members declared protected can be accessed only within the
class itself and by inherited and parent classes. Members
declared as private may only be accessed by the class that
defines the member.
§ Property Visibility
§ Class properties must be defined as public, private, or
protected. If declared using var, the property will be defined
as public.
Objects within Objects
class pakaian { class manusia {
public $warna public $nama;
= 'merah'; public $baju = new pakaian;
}
public function
__construct( $nama ) {
$this->nama = $nama;
}
}
Objects within objects example
$susan = new manusia('Susan');
$susan->baju = new pakaian;

echo $susan->nama
. ' memakai baju warna '
. $susan->baju->warna;

Output:
Susan memakai baju warna merah
Abstract Class
§ It's a kind "father" that must be inherited to be used.
Classes that inherit differ from them only in the
abstract methods and can access the methods of the
parent class using the keyword parent.
§ Features:
§ can not be instantiated
§ methods can be abstract (not implemented)
§ methods may be not abstract (implemented)
§ a class can inherit from a single abstract class
Abstract Class
abstract class Binatang
{
abstract protected function bicara();

// Common method (shared)


public function garukGaruk() {
echo "garuk garuk…";
}
}
Extending Abstract Class
class Kucing extends Binatang class Anjing extends Binatang
{ {
public function bicara() { public function bicara() {
echo "Meong…" echo "Guk…"
} }
} }

$anggora = new Binatang; // Err $herder = new Binatang; // Err


$anggora = new Kucing; $herder = new Anjing;
$anggora->bicara(); // meong… $herder->bicara(); // Guk…
$anggora->garukGaruk(); $herder->garukGaruk();
// garuk garuk… // garuk garuk…
Interface
§ The clearest definition is that an interface is a
contract.
§ Features:
§ All classes that implement an interface must develop all the
methods that have been defined
§ The class implementing the interface must use the exact
same method signatures as are defined in the interface. Not
doing so will result in a fatal error
§ All methods declared in an interface must be public, this is
the nature of an interface
§ A class can implement more than one interface
§ An interface can be used by the Type Hinting
Polymorphism
§ Polymorphism is the ability (in programming) to
present the same interface for differing
underlying forms (data types).
Polymorphism Example
interface binatang {
public function bicara();
}

class kucing class anjing


implements binatang { implements binatang {
public function bicara() { public function bicara() {
echo "Meong…"; echo "Guk… Guk…";
} }
} }
Polymorphism Example
<?php

$hewan = new kucing; // hewan as kucing


$hewan->bicara(); // Meong...
$hewan = new anjing; // hewan as anjing
$hewan->bicara(); // Guk... Guk...

?>
Notes
§ PHP have "final" keyword to prevent sub-class
method overriding.
§ "final" keyword can be implemented on
properties, methods and classes
§ final class cannot be inherited
§ objects are destroyed in the end of PHP scripts.
§ Like regular variables, it is possible to explicitly
destroy an object using the unset() function.
A copy, or not a copy..
§ Entire objects can be passed as arguments to
functions, and can use all methods/variables
within the function.
§ Remember however… like functions the object is
COPIED when passed as an argument unless you
specify the argument as a reference variable
&$variable
Exercise
Binatang § Implements the class
+Nama
+Warna diagram in PHP
+Bicara()
+Makan()
+GarukGaruk()
§ Create a Unit Test script
to test all implemented
methods and attibutes
(properties).

Bebek Kucing
+Makan() +Makan()
0..*
+Bicara() +Bicara() Kutu
+Berenang() +Berlari() 1 +Melompat()
+TambahKutu()
Task
Task (2)
§ Ubah diagram class tersebut ke dalam kode program
PHP.
§ Penilaian diberikan pada keakuratan dan kesesuaian kode
program dengan diagram class

§ Body dari tiap-tiap method dapat diisi dengan bebas


namun tetap sesuai dengan konteks diagram class
§ Diperbolehkan untuk mengembangkan kasus dan
skenario yang diberikan, dan dinilai sebagai nilai plus
Task (3)
§ Demonstrasikan penggunaan class-class tersebut
dalam sebuah skenario kode program dengan
bahasa pemrpgraman PHP yang
memperlihatkan:
§ Abstraksi class Kendaraan
§ Polimorfisme pada objek MobilBalap dan objek SUV
§ Ketika sebuah Mobil (bisa berupa objek MobilBalap
atau SUV) bergerak, maka seluruh objek AsRoda-nya
berputar dan objek Roda yang melekat pada AsRoda-
nya juga berputar.
Rules
§ Setiap class hanya didefinisikan dalam satu file PHP
§ Skenario program yang menggunakan seluruh class
tuliskan dalam sebuah file dengan nama file
program.php
§ Kerjakan secara perorangan! Plagiasi tidak
diperbolehkan. Kecurangan pada pengerjaan tugas,
quiz, UTS, UAS dapat berakibat pada gugurnya
seluruh mata kuliah yang diprogram dalam satu
semester.
Submission
§ Kirimkan seluruh file .php ke dalam sebuah file
.zip dengan nama file phpoop.zip sebagai
attachment email dengan subject:
Tugas VII PHP OOP
§ Tugas paling lambat diterima sebelum hari
Selasa, 17 Mei 2016 pukul 22:00 WIB
§ Ketentuan tugas lain yang telah diuraikan dalam
kesepakatan kuliah (Pertemuan I) tetap berlaku

Anda mungkin juga menyukai