💻

PHP Object-Oriented Programming

Jul 14, 2024

PHP Object-Oriented Programming

Pengantar

  • Pengajar: Eko Kurniawan Khannedy
  • Pengalaman kerja: 10+ tahun
  • Lokasi kerja: Technical Architect di e-commerce Indonesia
  • Konten: Programming di YouTube channel Programmer Zaman Now
  • Kontak: Telegram @Kennedy
  • Media Sosial: Facebook, Instagram, YouTube, Telegram

Ikhtisar Materi

  • Topik: PHP Object-Oriented Programming (OOP)
  • Pra-syarat: Pahami PHP dasar
  • Referensi: Video PHP dasar di channel Programmer Zaman Now

Key Concepts OOP

  • Obyek: Data komposit dengan properties dan methods
  • Class: Blueprint/Prototype untuk membuat object

Obyek dan Class

  • Obyek: Data yang memiliki field/properties dan methods (function/behevior)
  • Class: Cetakan untuk membuat obyek
  • Properties mirip variabel yang melekat pada obyek
  • Methods didefinisikan dalam class, bisa public/private

Contoh Class Person

class Person { public $name; public $address; public $country = 'Indonesia'; } $person1 = new Person(); $person1->name = 'Eko'; $person1->address = 'Subang';

Penulisan Class

  • Menggunakan kata kunci class, format CamelCase
  • Nama file tidak harus sama dengan nama class (PHP fleksibel)

Static Properties dan Methods

  • Static: Menempel pada kelas, tidak perlu instansiasi objek
  • Mengakses dengan :: tanpa membuat objek terlebih dahulu

Contoh Static Method

class MathHelper { public static function sum($a, $b) { return $a + $b; } } $result = MathHelper::sum(1, 2); // Output: 3

Inheritance (Pewarisan)

  • Inheritance: Mewariskan properti dan method dari class parent ke child
  • Keywords: extends
  • Contoh:
class Manager { public $name; public function sayHello($name) { echo "Hello $name, my name is $this->name"; } } class VicePresident extends Manager { }
  • Polymorphism: Kemampuan objek berubah bentuk ke turunan/class lain
  • Contoh:
$manager = new Manager(); $vp = new VicePresident(); $manager = $vp; // Polymorphism

Interface

  • Defines a contract for methods in a class
  • Implements keyword used

Traits

  • Reusable set of methods that can be included in multiple classes
  • Used via use keyword

Abstract Class

  • Class that cannot be instantiated; used as a base class
  • Contain abstract methods that must be defined in child classes

Exception Handling

  • try-catch: To handle exceptions without stopping script execution
  • finally: Execute code regardless of exception being thrown or not
  • Creating custom exceptions: Extend the Exception class

Example Custom Exception

class ValidationException extends Exception { } try { throw new ValidationException("Error!"); } catch (ValidationException $e) { echo $e->getMessage(); } finally { echo "Done."; }

Reflection

  • Reflection: Inspect and manipulate classes, properties, and methods at runtime
  • Used for debugging, code analysis, and framework development

Example Usage

$reflectionClass = new ReflectionClass('Person'); $properties = $reflectionClass->getProperties(); foreach ($properties as $property) { echo $property->getName(); }

Regular Expressions

  • Used for pattern matching in strings
  • Functions: preg_match, preg_replace, preg_split

Example Usage

if (preg_match('/Eko|awan|Ed/i', 'Eko Kurniawan Kennedy', $matches)) { print_r($matches); }

Date-Time Handling

  • DateTime class: For handling dates and times in PHP
  • Functions like format, setDate, setTime, etc.

Example Usage

$date = new DateTime(); $date->setDate(1990, 1, 20); $date->setTime(10, 10, 10); echo $date->format('Y-m-d H:i:s'); // Output: 1990-01-20 10:10:10

Summary

  • OOP basics: Objects, Classes, Properties, Methods, Inheritance
  • Exception handling using try-catch-finally
  • Advanced topics including Traits, Abstract Classes, Interfaces
  • Reflection for runtime code manipulation
  • Regular Expressions for pattern matching
  • Date-Time handling for manipulating date and time in applications

Further Learning:

  1. PHP Database: Integrasi aplikasi dengan database
  2. PHP Web Development: Membuat aplikasi berbasis web dengan PHP
  3. PHP Composer: Library management tool for PHP
  4. PHP Unit Testing: Menulis unit test untuk aplikasi PHP agar lebih andal

Referensi Tambahan: