OOP PHP 5.3 Faruoqi
Basic •  Classes and objects •  Constructor methods •  Inheritance •  Visibility
Class Content Variables di deklarasikan dengan “visibility operator” dengan standart naming convention variable Constants di deklarasikan dengan “const” keyword Methods di deklarasikan dengan “function” keyword Class hanya mempunyai satu tujuan penggunaan
Contoh class ShopProduct {  public $title ; private $producerMainName  ;  private $producerFirstName  ;  public $price ;  public function __construct( $title, $firstName, $mainName, $price ) {  $this->title  = $title;  $this->producerFirstName = $firstName;  $this->producerMainName  = $mainName;  $this->price  = $price;  } public function getProducer() {  return "{$this->producerFirstName}".  " {$this->producerMainName}";  }  }
Object Type Hinting Berguna untuk mengecek argument yang dimasukkan ke suatu fungsi adalah instance dari class tertentu . Jadi hanya argument bertipe objek yang boleh masuk
Buat ShopProductWriter class ShopProductWriter {  public function write(ShopProduct $shopProduct ) {  $str  = "{$shopProduct->title}: " .  $shopProduct->getProducer() .  " ({$shopProduct->price})\n";  print $str;  }  } $product= new ShopProduct( "My Antonia", "Willa", "Cather", 5.99 );  $writer = new ShopProductWriter();  $writer->write( $product1 );
Inheritance class CdProduct extends ShopProduct {  public $playLength;  public function __construct(  $title, $firstName, $mainName, $price, $playLength ) {  parent::__construct(  $title, $firstName, $mainName, $price );  $this->playLength = $playLength;  }  public function getPlayLength() {  return $this->playLength;  }  public function getSummaryLine() {  $base  = "{$this->title} ( {$this->producerMainName}, ";  $base .= "{$this->producerFirstName} )";  $base .= ": playing time - {$this->playLength}";  return $base;  }  }
Static Method & Property Fungsi atau properti yang bisa diakses tanpa harus me referensi ke objek instance dari class tersebut. Jadi langsung mereferensi ke class nya
Contoh static  class StaticClass{  static public $nama =“oqi”;  public $say = “hallo”; static public function sayHello() {  return self::$nama; }  }  Echo StaticClass::sayHello(); Echo  StaticClass::$nama;
Abstract Class Class yang tidak bisa di instance langsung,jadi hanya sebagai API atau template dari class turunannya Semua fungsi di abstract class yang mempunyai keyword abstract,harus dipunyai oleh class turunannya
Contoh abstract class abstract class ShopProductWriter {  protected $products = array();  public function addProduct( ShopProduct $shopProduct ) {  $this->products[]=$shopProduct;  }  abstract public function write();  } class TextProductWriter extends ShopProductWriter{  public function write() {  $str = "PRODUCTS:\n";  foreach ( $this->products as $shopProduct ) {  $str .= $shopProduct->getSummaryLine()."\n";  }  print $str;  }  }
Interface Adalah kumpulan dai fungsi yang harus dipakai jka di implementasikan oleh suatu class interface Chargeable {  public function getPrice();  } class ShopProduct implements Chargeable {  // ...  public function getPrice() {  return ( $this->price - $this->discount );  }  // ...  }
Design pattern Aggregation Composition
Design Pattern Decouple Singleton
Contoh Singleton Sederhana class Single { private static $_ins = null; const CONFIG = 'ok juga'; protected function __construct() { } public static function getInstance(){ if(!isset(self::$_ins)){ self::$_ins = new self(); } return self::$_ins; } public function coba(){ return  'ok'; } }
Cara make class singleton echo Single::getInstance()->coba();
Status 20% ready to learn Zend Framework. Tapi kalau bisa diterusin lagi belajar Oop nya di rumah PR nya: lazy loading late static binding factory pattern
Bersambung ke next slide faruoqi

Oop php 5

  • 1.
    OOP PHP 5.3Faruoqi
  • 2.
    Basic • Classes and objects • Constructor methods • Inheritance • Visibility
  • 3.
    Class Content Variablesdi deklarasikan dengan “visibility operator” dengan standart naming convention variable Constants di deklarasikan dengan “const” keyword Methods di deklarasikan dengan “function” keyword Class hanya mempunyai satu tujuan penggunaan
  • 4.
    Contoh class ShopProduct{ public $title ; private $producerMainName ; private $producerFirstName ; public $price ; public function __construct( $title, $firstName, $mainName, $price ) { $this->title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } public function getProducer() { return "{$this->producerFirstName}". " {$this->producerMainName}"; } }
  • 5.
    Object Type HintingBerguna untuk mengecek argument yang dimasukkan ke suatu fungsi adalah instance dari class tertentu . Jadi hanya argument bertipe objek yang boleh masuk
  • 6.
    Buat ShopProductWriter classShopProductWriter { public function write(ShopProduct $shopProduct ) { $str = "{$shopProduct->title}: " . $shopProduct->getProducer() . " ({$shopProduct->price})\n"; print $str; } } $product= new ShopProduct( "My Antonia", "Willa", "Cather", 5.99 ); $writer = new ShopProductWriter(); $writer->write( $product1 );
  • 7.
    Inheritance class CdProductextends ShopProduct { public $playLength; public function __construct( $title, $firstName, $mainName, $price, $playLength ) { parent::__construct( $title, $firstName, $mainName, $price ); $this->playLength = $playLength; } public function getPlayLength() { return $this->playLength; } public function getSummaryLine() { $base = "{$this->title} ( {$this->producerMainName}, "; $base .= "{$this->producerFirstName} )"; $base .= ": playing time - {$this->playLength}"; return $base; } }
  • 8.
    Static Method &Property Fungsi atau properti yang bisa diakses tanpa harus me referensi ke objek instance dari class tersebut. Jadi langsung mereferensi ke class nya
  • 9.
    Contoh static class StaticClass{ static public $nama =“oqi”; public $say = “hallo”; static public function sayHello() { return self::$nama; } } Echo StaticClass::sayHello(); Echo StaticClass::$nama;
  • 10.
    Abstract Class Classyang tidak bisa di instance langsung,jadi hanya sebagai API atau template dari class turunannya Semua fungsi di abstract class yang mempunyai keyword abstract,harus dipunyai oleh class turunannya
  • 11.
    Contoh abstract classabstract class ShopProductWriter { protected $products = array(); public function addProduct( ShopProduct $shopProduct ) { $this->products[]=$shopProduct; } abstract public function write(); } class TextProductWriter extends ShopProductWriter{ public function write() { $str = "PRODUCTS:\n"; foreach ( $this->products as $shopProduct ) { $str .= $shopProduct->getSummaryLine()."\n"; } print $str; } }
  • 12.
    Interface Adalah kumpulandai fungsi yang harus dipakai jka di implementasikan oleh suatu class interface Chargeable { public function getPrice(); } class ShopProduct implements Chargeable { // ... public function getPrice() { return ( $this->price - $this->discount ); } // ... }
  • 13.
  • 14.
  • 15.
    Contoh Singleton Sederhanaclass Single { private static $_ins = null; const CONFIG = 'ok juga'; protected function __construct() { } public static function getInstance(){ if(!isset(self::$_ins)){ self::$_ins = new self(); } return self::$_ins; } public function coba(){ return 'ok'; } }
  • 16.
    Cara make classsingleton echo Single::getInstance()->coba();
  • 17.
    Status 20% readyto learn Zend Framework. Tapi kalau bisa diterusin lagi belajar Oop nya di rumah PR nya: lazy loading late static binding factory pattern
  • 18.
    Bersambung ke nextslide faruoqi