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('Joko');
ech o $m ah asisw a-> n am a; // Joko
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.
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
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 class 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...
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