Skip to content
  • Home
  • Agro
    • Agro
    • Akuaponik
    • Hidroponik
    • Ayam Kampung
  • DIY
    • DIY
    • Rumah Kampung
  • Tech
    • Tech
    • Titanium Mobile
  • Media
    • Media
    • Downloads
    • Dashcam Drive
  • Misc
    • Pelbagai
    • Makanan
    • Fotografi
    • Idea
    • E-mau
  • Home
  • Agro
    • Agro
    • Akuaponik
    • Hidroponik
    • Ayam Kampung
  • DIY
    • DIY
    • Rumah Kampung
  • Tech
    • Tech
    • Titanium Mobile
  • Media
    • Media
    • Downloads
    • Dashcam Drive
  • Misc
    • Pelbagai
    • Makanan
    • Fotografi
    • Idea
    • E-mau
blog.azwan082.my


Blog ini tidak lagi dikemaskini, sila ke
👉 azwan082.my 👈
untuk dapatkan kandungan terbaru.

#Idea  #Object oriented  #Object oriented programming  #OOP  #Pattern  #PHP  #Software

OOP the right way

On 15 November 2012 in Pelbagai

Most of tutorials on object oriented programming tells the reader about how to do it rather than why we have to write it that way. This post attempts to explain why we write object oriented code the way it is.

Why object oriented? Object oriented is a way to write an application in components to reduce code repetition, enhance maintainability (able to unit test) & to let developer to focus on specific parts of app (separation of concern). Therefore, an app must be structured into components (component-based application).

In explaining class & objects in oop, it’s better to use real world example. Here I’ll take cache component of a web application. The basic functionality of cache component is to store app level data into temporary storage & retrieve it quickly. Now we can assume the basic class of a cache component is like this:

class Cache {
	public function get($key) {}
	public function set($key, $data, $expired) {}
	public function delete($key) {}
}

Notice we’re using public method. Public methods are used to expose functionality of a component for other components to use. This is the basic of designing an API, we’re defining how to use a component, what input it receives & what the output it’s expected to produce

class Cache {
	/**
	 * @param string $key
	 * @return array|false
	 */
	public function get($key) {}
	/**
	 * @param string $key
	 * @param mixed $data
	 * @param string|int $expired
	 */
	public function set($key, $data, $expired) {}

	// ...
}

How about the implementation? We know that cache component can use many types of backend, such as file based, SQLite, Memcached, Redis etc. Here is where inheritance is useful. Inheritance is used to either implement methods to a specific condition or to change the way data is processed (override).

class Cache {
	// ... public methods ...

	protected function purge() {}
	protected function getExpirationTime() {}
	protected function formatInputData() {}
	protected function formatOutputData() {}
}

Here we are using protected method. Protected is used on methods that are not going to be used by other components – these methods are going to be override by child class & used within the class itself. Let’s take a look at the example of child class implementations:

// using sqlite backend
class SQLiteCache extends Cache {
	private $connection;
	public function __construct() {
		parent::__construct();
		$this->connect();
	}
	private function connect() {
		// connect to database
		$this->connection = new PDO('sqlite::memory:');
	}
}

// using memcached backend
class MemcachedCache extends Cache {}

// using file-based backend
class FileCache extends Cache {
	// expose specific hidden methods to let extended class
	// to manipulate the data based on its backend
	protected function formatInputData() {
		if (!is_string($this->data)) {
			$this->data = serialize($this->data);
		}
	}
}

In SQLiteCache class, it has its own connect() method, that’s going to be used in this class only. That’s why it’s declared private. We may have connect() method in MemcachedCache to connect to memcached server, however the implementation is totally different from SQLiteCache, so we may write those methods separately without needing a parent class. Only create a parent class to combine child classes that has similar code (reduce code repetition).

In FileCache, we’re implementing formatInputData() because we couldn’t store PHP variable other than strings into file, so we serialize() the vars. This is the appropriate way to implement hook pattern (like in WordPress). Since this methods is declared in our base Cache class, this method can be called before storing the data into cache store, with different type of backend may already manipulate the data format to suite the backend requirement.

Let’s say this code is packaged in a module released by other developer and we’re not satisfied with the MemcachedCache implementation, we can extends that class and write our own implementation. This way, we’re not disturbing the module code & it’s important so that we don’t have to hack the module code again every time we want to update it.

class BetterMemcachedCache extends MemcachedCache {
	protected function getExpirationTime() {
		// return time-to-live instead of expiration timestamp
		return strtotime($this->expired, 0);
	}
}

Summary: Write OOP code in components, public methods are the API of the components, protected methods are for specific implementation & private methods are for usage within that class only. In general, we may have a lot of protected methods (to achieve hook pattern) and a few private methods (only to group repetitious code).

Share this post:
  • Share
  • Tweet
  • LinkedIn

Related posts:

  • Singleton pattern
  • Object oriented Javascript with CommonJS in Titanium app
  • PHP timezone handling
  • Formula E bakal dianjurkan di Kuching?
  • Singleton class
  • session_regenerate_id() old data not copied to new session ID
  • PHP Data Validation
  • New PHP
  • Page Controller Pt.2
  • Macam betul-betuuul

Filed under Pelbagai with tags Idea, Object oriented, Object oriented programming, OOP, Pattern, PHP, Software

Post navigation

Previous Post Previous post:
Designing web page for Android webview
Next Post Next post:
Setup Titanium Studio environment for developing Titanium module (Android) on Windows

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Media sosial

  • facebook
  • instagram
  • twitter
  • pinterest
  • youtube

Carian

Artikel popular

  • Saiz standard kayu, papan dan plywood
  • Pemasangan dinding plywood rumah kampung secara solo
  • 3 Jenis Asas Sistem Hidroponik Yang Digunakan Dalam Akuaponik Serta Variasinya
  • Kepincangan dalam menggunakan paip PVC sebagai saluran NFT hidroponik
  • Cara membuat sistem NFT untuk akuaponik – Saliran masuk
  • Cara pasang pintu rumah
  • Jenis skru kayu
  • Pemasangan pagar reban ayam kampung

Artikel terkini

  • 2 video #emau dari 2020/0221 February 2020
  • Gaya hidup minimalis14 February 2020
  • How to sync Mac OS Photos Library to an external disk storage17 January 2020
  • Sejarah dividen KWSP10 January 2020
  • Sejarah dividen Tabung Haji10 January 2020
  • Sejarah dividen ASB10 January 2020
  • Selamat dekad baru 2020-an3 January 2020
  • Dashcam Drive #17 – Taman Danu Serian → Ranchan Recreational Park1 January 2020

Tag

1N2D Akuaponik Android Apache Ayam Kampung Bash C# Cache Cili CommonJS Controller Dashcam Drive E-mau Fedora Fotografi Git Gnome Happy Together Hidroponik Idea Invincible Youth iOS Java Javascript Ke Indonesia Ke Kita? Ke Jepun Ke Kita? Ke Korea Ke Kita? Kewangan ListView Makanan Mr. Bean PHP Python Rant Roundtable Plus RPM Rumah Kampung Star Golden Bell Subversion SVN Titanium Mobile Titanium Module Titanium Studio windows phone WrestleMania

Arkib

Perihal

@azwan082

Seorang pembangun perisian (software developer) untuk peranti mudah alih (mobile devices) dan laman web, menggunakan bahasa pengaturcaraan Swift (iOS), Java (Android), PHP, MySQL dan Javascript (aplikasi web). Blog ini adalah tempat untuk saya berkongsi perkara, minat dan projek sampingan, seperti berkebun, kerja² kayu DIY, video dashcam drive dan pelbagai lagi.

© 2021 blog.azwan082.my

Back to top
sponsored