Eduardo Arsand

Understanding Abstraction

42

Abstraction is the practice of hiding implementation details behind a simplified interface, so developers can work with complex systems without needing to understand all their inner workings. It focuses on what something does, not how it does it.

Key Benefits of Abstraction

  • Reduces Complexity: Users of the code deal only with what they need. Example: Using array_map() in PHP without worrying about the internal loop.
  • Improves Reusability: Abstract components can be reused across different projects or modules. Example: A PaymentGateway interface used for multiple providers like PayPal or Stripe.
  • Facilitates Maintenance: Changes in implementation don’t affect other parts of the system as long as the interface stays the same.
  • Enhances Flexibility: Multiple implementations can coexist behind a single abstract interface. Example: Logging to a file, database, or external service without changing client code.

Examples

1. PHP Class Abstraction

<?php
	//Abstract class defines behavior, hides details
	abstract class Logger {
		public abstract function log(string $message);
	}

	//Concrete implementation hides specifics
	class FileLogger extends Logger {
		public function log(string $message) {
			file_put_contents('log.txt', $message.PHP_EOL, FILE_APPEND);
		}
	}

	class DatabaseLogger extends Logger {
		public function log(string $message) {
			//code to store log in database
		}
	}

	function processUserAction(Logger $logger) {
		$logger->log("User performed an action");
	}

	$logger = new FileLogger();
	processUserAction($logger); //Works the same if we swap in DatabaseLogger
?>

2. HTML Semantic Abstraction

<article>
	<h2>Blog Post Title</h2>
	<p>This is the summary of the post...</p>
</article>

The <article> abstracts the concept of a self-contained content block. You don’t need to worry about layout or styling.

3. Function Abstraction

<?php
	//Hides internal steps
	function calculateDiscount(float $price, float $rate): float {
		return $price - ($price * $rate);
	}

	//You don’t need to know the formula every time
	$total = calculateDiscount(100, 0.2);
?>

4. Abstraction Across Layers (PHP + HTML)

<?php
	//PHP backend abstraction
	interface ContentProvider {
		public function getTitle(): string;
		public function getBody(): string;
	}

	class BlogPost implements ContentProvider {
		private $title;
		private $body;

		public function __construct($title, $body) {
			$this->title = $title;
			$this->body = $body;
		}

		public function getTitle(): string {
			return $this->title;
		}

		public function getBody(): string {
			return $this->body;
		}
	}

	//Frontend HTML consumes abstraction
	function renderArticle(ContentProvider $content) {
		echo "<article>";
		echo "<h2>" . $content->getTitle() . "</h2>";
		echo "<p>" . $content->getBody() . "</p>";
		echo "</article>";
	}

	$post = new BlogPost("Hello World", "This is an abstracted content.");
	renderArticle($post);
?>

Here the PHP backend abstracts the content source, and the frontend only cares about ContentProvider. You could swap BlogPost with NewsItem and the HTML rendering stays the same.

Tips for Using Abstraction

  1. Abstract when behavior repeats or may change
    If multiple classes or functions share similar behavior but differ in implementation, abstraction reduces duplication.
  2. Abstract for clarity and maintainability
    Hiding complex logic behind a simple interface makes code easier to understand for other developers (or your future self).
  3. Avoid over-abstraction
    Too many layers can make code harder to follow. If the implementation is simple and unlikely to change, abstraction may be unnecessary.
  4. Favor interfaces over concrete implementations
    Define what should happen, not how. This allows multiple interchangeable implementations.
  5. Think in terms of contracts
    An abstraction defines a contract: “if you follow these rules, your component can be swapped freely.”

How to Decide if Something Needs an Abstraction Layer

Ask yourself:

  • Will this component need multiple implementations in the future?
  • Will the internal logic likely change independently of its users?
  • Do you want to decouple modules so changes in one don’t break others?
  • Will reusability be improved by hiding details behind an interface?

If the answer is yes to any, an abstraction layer is usually beneficial. If not, keep it simple.

This approach makes your code scalable, reusable, and easier to maintain, while preventing unnecessary complexity.


Comments ({{ modelContent.total_comments }})