pemrograman web n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Pemrograman Web PowerPoint Presentation
Download Presentation
Pemrograman Web

Loading in 2 Seconds...

play fullscreen
1 / 33

Pemrograman Web - PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on

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?.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Pemrograman Web' - booth


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
pemrograman web

Pemrograman Web

Object Oriented Programming in PHP 5

what is a function
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
What is a class?
  • Conceptually, a class represents an object, with associated methods and variables
class definition example
Class Definition Example

<?php

// filename: manusia.class.php

class manusia {

public $nama;

public function menyapa() {

echo 'Halo!'; }

}

?>

class defintion
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
Class Usage

<?php

require('manusia.class.php');

$susan = new manusia;

$susan->nama = 'Susan';

echo $susan->nama

. ' jikamenyapa, berkata: '

. $susan->menyapa();

?>

using attributes within the class
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 manusia {

public $nama;

public function menyapa() {

echo $this->nama . ' bilang Halo!'; }

}

$susan = new manusia;

$susan->nama = 'Susan';

$susan->menyapa();

constructor methods
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
Constructor Example

<?php

class manusia {

public $nama;

public function __construct($nama) {

$this->nama = $nama;

}

public function menyapa() {

echo $this->nama . ' bilang Halo!'; }

}

?>

constructor example1
Constructor Example

<?php

$susan = new manusia('Susan');

$susan->menyapa();

?>

Output:

Susan bilang Halo!

class scope
Class Scope
  • Like functions, each instantiated object has its own local scope.

<?php

$mahasiswi= new manusia('Susan');

$mahasiswa = new manusia('Adi');

echo $mahasiswa->nama; // Adi

$mahasiswa->nama = "Budi";

echo $mahasiswi->nama; // Susan;

?>

inheritance
Inheritance
  • The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes.

manusia

parent

children

mahasiswa

dosen

inheritance1
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 mahasiswa inherits the variable nama and method menyapa from the manusia class, and can add extra ones…
inheritance example
Inheritance example

<?php

class mahasiswa extends manusia {

public function __construct($nama){

$this->nama = $nama;

}

public $tugas = 'belajar';

}

$susan = new mahasiswa('Susan');

echo $susan->menyapa()

. ' ketikasedang ' . $susan->tugas;

Output: Susan bilang Halo! ketikasedangbelajar

method override
Method Override
  • Mahasiswaselaluberkata 'Hei!' ketikamenyapa.

<?php

class mahasiswa extends manusia {

public function menyapa(){

echo $this->nama . ' bilangHei!';

}

}

$susan = new mahasiswa('Susan');

echo $susan->menyapa();

Output: Susan bilangHei! ketikasedangbelajar

child constructors
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
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
Visibility Example

class MyClass

{

public $public = 'Public';

protected $protected = 'Protected';

private $private = 'Private';

function printHello()

{

echo $this->public;

echo $this->protected;

echo $this->private;

}

}

$obj = new MyClass();

echo $obj->public; // Works

echo $obj->protected; // Fatal Error

echo $obj->private; // Fatal Error

$obj->printHello(); // Shows Public, Protected and Private

class MyClass2 extends MyClass

{

protected $protected = 'Protected2';

function printHello()

{

echo $this->public;

echo $this->protected;

echo $this->private;

}

}

$obj2 = new MyClass2();

echo $obj2->public; // Works

echo $obj2->private; // Undefined

echo $obj2->protected; // Fatal Error

// Shows Public, Protected2, Undefined

$obj2->printHello();

objects within objects
Objects within Objects
  • It is perfectly possible to include objects within another object

<?php

class pakaian {

public $warna= 'merah';

}

class manusia {

public $nama;

public $baju;

public function __construct( $nama ) {

$this->nama = $nama;

}

}

objects within objects example
Objects within objects example

<?php

$susan = new manusia('Susan');

$susan->baju = new pakaian;

echo $susan->nama

. ' memakaibajuwarna '

. $susan->baju->warna;

?>

Output:

Susan memakaibajuwarnamerah

encapsulation
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
Encapsulation Example

<?php

class pakaian {

public $warna = 'merah';

}

class manusia {

private $baju;

public function __construct() {

$this->baju = new pakaian;

$this->baju->warna = 'biru';

}

public function warnaBaju() {

return $this->baju->warna;

}

}

$susan = new manusia();

echo 'Susan memakaibajuberwarna ' . $susan->warnaBaju();

Output: Susan memakaibajuberwarnabiru

abstract class
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 class1
Abstract Class

abstract class Binatang

{

abstract protected function bicara();

// Common method (shared)

public function garukGaruk() {

echo "garukgaruk…";

}

}

extending abstract class
Extending Abstract Class

class Kucing extends Binatang

{

public function bicara() {

echo "Meong…"

}

}

$anggora = new Binatang; // E

$anggora = new Kucing;

$anggora->bicara(); // meong…

$anggora->garukGaruk();

// garukgaruk…

class Anjing extends Binatang

{

public function bicara() {

echo "Guk…"

}

}

$herder = new Binatang; // E

$herder = new Anjing;

$herder->bicara(); // Guk…

$herder->garukGaruk();

// garukgaruk…

interface
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
  • Polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).
polymorphism example
Polymorphism Example

<?php

interface binatang {

public function bicara();

}

class kucing implements binatang {

public function bicara() {

echo "Meong…";

}

}

class anjing implements binatang {

public function bicara() {

echo "Guk… Guk…";

}

}

polymorphism example1
Polymorphism Example

<?php

$hewan = new kucing;

$hewan->bicara(); // Meong...

$hewan = new anjing;

$hewan->bicara(); // Guk... Guk...

?>

latihan
Latihan
  • Buatlahsebuah interface kendaraanyang memiliki: method:
    • bukaPintu();
    • jumlahRoda();
  • Buatlahkelasmobildan bus yang mengimplementasikan interface kendaraantersebut.
  • Mobil dan bus memilikiatribut $rodadan $pintu;
  • Nilaiatribut $rodamobil = 4; bus = 6;
  • Nilaiatribut $pintumobil = 5; bus = 3;
  • Perlihatkan polymorphism padakelasmobildan bus tersebut di atasuntuk method bukaPintu() danjumlahRoda()!
final keyword
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
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 unset() function.
a copy or not a copy
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 COPIEDwhen passed as an argument unless you specify the argument as a reference variable &$variable