Você está na página 1de 33

Pemrograman Web

Object Oriented Programming in PHP 5

What is a function?

Conceptually, what does a function represent?


give the function something
(arguments), it does something with
them, and then returns a result

Action or Method

What is a class?

Conceptually, a class represents an object, with


associated methods and variables

Class Definition Example


< ?p h p
// f i
len am e: m an u sia.class.p h p
class m an u sia {
p u b lic $n am a;
p u b lic fu n ction m en yap a() {
ech o 'H alo!';
}
}
?>

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

Class Usage
< ?p h p
req u ire('m an u sia.class.p h p ');
$su san = n ew m an u sia;
$su san -> n am a = 'S u san ';
ech o $su san -> n am a
. ' jika m en yap a, b erkata: '
. $su san -> m en yap a();
?>

Using attributes within the class..

If you need to use the class variables within any class


actions/methods, use the special variable $this in the definition:

class m an u sia {
p u b lic $n am a;
p u b lic fu n ction m en yap a() {
ech o $ th is-> n am a . ' b ilan g H alo!';
}
}
$ su san = n ew m an u sia;
$ su san -> n am a = 'S u san ';
$ su san -> m en yap a();

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 __con stru ct name.
Remember.. if the constructor requires arguments,
they must be passed when it is instantiated!

Constructor Example
< ?p h p
class m an u sia {
p u b lic $n am a;
p u b lic fu n ction __con stru ct($n am a) {
$th is-> n am a = $n am a;
}
p u b lic fu n ction m en yap a() {
ech o $th is-> n am a . ' b ilan g H alo!';
}
}
?>

Constructor Example
< ?p h p

$su san = n ew m an u sia('S u san ');


$su san -> m en yap a();

?>
O u tp u t:
S u san b ilan g H alo!

Class Scope

Like functions, each instantiated object has its own local scope.

< ?p h p
$m ah asisw i = n ew m an u sia('S u san ');
$m ah asisw a = n ew m an u sia('A d i');
ech o $m ah asisw a-> n am a; // A d i
$m ah asisw a-> n am a = "B u d i";
ech o $m ah asisw i-> n am a; // S u san ;
?>

Inheritance

The real power of using classes is the property of


inheritance creating a hierarchy of interlinked
classes.

manusia

mahasiswa

dosen

parent
children

Inheritance

The child classes 'inherit' all the methods and


variables of the parent class, and can add extra ones
of their own.
e.g. the child classes m ahasisw a inherits the
variable nam a and method m enyapa from the
m anusia class, and can add extra ones

Inheritance example
< ?p h p
class m ah asisw a exten d s m an u sia {
p u b lic fu n ction __con stru ct($n am a){
$th is-> n am a = $n am a;
}
p u b lic $tu g as = 'b elajar';
}
$su san = n ew m ah asisw a('S u san ');
ech o $su san -> m en yap a()
. ' ketika sed an g ' . $su san -> tu g as;
O u tp u t: S u san b ilan g H alo! ketika sed an g b elajar

Method Override

Mahasiswa selalu berkata 'Hei!' ketika menyapa.

< ?p h p
class m ah asisw a exten d s m an u sia {

p u b lic fu n ction m en yap a(){


ech o $th is-> n am a . ' b ilan g H ei!';
}
}
$su san = n ew m ah asisw a('S u san ');
ech o $su san -> m en yap a();
O u tp u t: S u san b ilan g H ei! ketika sed an g b elajar

Child Constructors?

If the child class possesses a constructor function, it


is executed and any parent constructor is ignored.
If the child class does not have a constructor, the
parent's constructor is executed.
If the child and parent does not have a constructor,
the grandparent constructor is attempted
etc.

Class Visibility

Visibility

The visibility of a property or method can be defined by


prefixing the declaration with the 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.

Visibility Example
class M yC lass
{
p u b lic $p u b lic = 'P u b lic';
p rotected $p rotected = 'P rotected ';
p rivate $p rivate = 'P rivate';
fu n ction p rin tH ello()
{
ech o $th is-> p u b lic;
ech o $th is-> p rotected ;
ech o $th is-> p rivate;
}
}
$ob j = n ew M yC lass();
ech o $ob j-> p u b lic; // W orks
ech o $ob j-> p rotected ; // Fatal Error
ech o $ob j-> p rivate; // Fatal Error
$ob j-> p rin tH ello(); // S h ow s P u b lic,
P rotected an d P rivate

class M yC lass2 exten d s M yC lass


{
p rotected $ p rotected = 'P rotected 2 ';
fu n ctio n p rin tH ello()
{
ech o $ th is-> p u b lic;
ech o $ th is-> p rotected ;
ech o $ th is-> p rivate;
}
}
$ ob j2 = n ew M yC lass2 ();
ech o $ ob j2 -> p u b lic; // W orks
ech o $ ob j2 -> p rivate; // U n d ef i
n ed
ech o $ ob j2 -> p rotected ; // Fatal Error
// S h ow s P u b lic, P rotected 2 , U n d ef i
n ed
$ ob j2 -> p rin tH ello();

Objects within Objects

It is perfectly possible to include objects within another object

< ?p h p
class p akaian {
p u b lic $w arn a = 'm erah ';
}
class m an u sia {
p u b lic $n am a;
p u b lic $b aju ;
p u b lic fu n ction __con stru ct( $n am a ) {
$th is-> n am a = $n am a;
}
}

Objects within objects example


< ?p h p
$su san = n ew m an u sia('S u san ');
$su san -> b aju = n ew p akaian ;
ech o $su san -> n am a
. ' m em akai b aju w arn a '
. $su san -> b aju -> w arn a;
?>
O u tp u t:
S u san m em akai b aju w arn a m erah

Encapsulation

Encapsulation is a way of storing an object or data as


a property within another object, so that the outer
object has full control over what how the internal
data or object can be accessed.
This, in combination with making the inner
object/property private, enables
information hiding.

Encapsulation Example
< ?p h p
class p akaian {
p u b lic $w arn a = 'm erah ';
}
class m an u sia {
p rivate $b aju ;
p u b lic fu n ction __con stru ct() {
$th is-> b aju = n ew p akaian ;
$th is-> b aju -> w arn a = 'b iru ';
}
p u b lic fu n ction w arn aB aju () {
retu rn $th is-> b aju -> w arn a;
}
}
$su san = n ew m an u sia();
ech o 'S u san m em akai b aju b erw arn a ' . $su san -> w arn aB aju ();
O u tp u t: S u san m em akai b aju b erw arn a b iru

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
ab stract class B in atan g
{
ab stract p rotected fu n ction b icara();
// C om m on m eth od (sh ared )
p u b lic fu n ction g aru kG aru k() {
ech o "g aru k g aru k ";
}
}

Extending Abstract Class


class K u cin g exten d s B in atan g
{
p u b lic fu n ction b icara() {

class A n jin g exten d s


B in atan g
{
p u b lic fu n ction b icara() {

ech o "M eon g "

ech o "G u k "

}
$an g g ora = n ew B in atan g ; // E
$an g g ora = n ew K u cin g ;

$h erd er = n ew B in atan g ; // E

$an g g ora-> b icara(); //


m eon g

$h erd er = n ew A n jin g ;

$an g g ora-> g aru kG aru k();

$h erd er-> g aru kG aru k();

// g aru k g aru k

$h erd er-> b icara(); // G u k


// g aru k g aru k

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
< ?p h p
in terface b in atan g {
p u b lic fu n ction b icara();
}
class ku cin g im p lem en ts b in atan g {
p u b lic fu n ction b icara() {
ech o "M eon g ";
}
}
class an jin g im p lem en ts b in atan g {
p u b lic fu n ction b icara() {
ech o "G u k G u k ";
}
}

Polymorphism Example
< ?p h p
$h ew
$h ew
$h ew
$h ew
?>

an = n ew ku cin g ;
an -> b icara(); // M eon g ...
an = n ew an jin g ;
an -> b icara(); // G u k... G u k...

Latihan

Buatlah sebuah interface kendaraan yang memiliki:


method:

bukaPintu();
jumlahRoda();

Buatlah kelas mobil dan bus yang


mengimplementasikan interface kendaraan tersebut.
Mobil dan bus memiliki atribut $roda dan $pintu;
Nilai atribut $roda mobil = 4; bus = 6;
Nilai atribut $pintu mobil = 5; bus = 3;
Perlihatkan polymorphism pada kelas mobil dan bus
tersebut di atas untuk method bukaPintu() dan
jumlahRoda()!

Final Keyword

PHP introduces "Final" keyword to prevent sub-class


method overriding.
"Final" keyword can be implemented on properties,
methods and classes
Final class means it cannot be inherited

Deleting objects

So far our objects have not been destroyed till the


end of our scripts..
Like variables, it is possible to explicitly destroy an
object using the u n set() 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
& $variab le

Você também pode gostar