PHP Tutorial Tutorials - PHP OOP Abstraction

PHP OOP Abstraction

PHP OOP Abstraction

Abstraction means to represent the essential feature to hide irrelevant details or internal implementation details, without detailing the background implementation or internal working detail.

Abstraction places the emphasis on what an object is or does rather than how it is represented or how it works.

Abstraction helps in building independent modules, lets you focus on what the object does instead of how it is done.

The person who uses that module does not need to bother about how the task is achieved or what exactly is happening in the background.
Modifying one independent module does not impact the other modules.

Abstraction is everywhere. One example is the phone. You interact with the phone by using only a few buttons. You don’t have to know how it work. Implementation changes(for example, a software update) does not impact you.

Abstract Class

In OOP, Abstract class is the concept described what to do and implementation gets completed when it is being realized by a subclass.

That is:
Abstract class: what to do
Subclass inherited from abstract class: How to do

Declaring Abstract Class

Abstract classes, which declared with the abstract keyword, cannot be instantiated, and any class that contains at least one abstract method must also be abstract.

<?php
abstract class AbstractClass
{
    // Force Extending class to define this method
    abstract protected function getValue();
    
    // Common method
    public function render() {
        echo $this->getValue() . "\n";
    }
}

// $obj = new AbstractClass(); // Fatal Error,Abstract class cannot be instantiated
?>

Methods(like getValue()) defined as abstract simply declare the method's signature - they cannot define the implementation. (They don't have a body,no curly brackets {})

The class inherited from abstract class must override all abstract method.So, Abstract method's visibility (getValue) can either be public or protected, but not private.

Abstract class can have non-abstract methods :render()

Abstract Class Rules

Abstract class example:

<?php
abstract class AbstractClass
{
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);
    abstract protected function prefixName($name);
}

When inheriting from an abstract class,

  • all methods marked abstract in the parent's class declaration must be defined by the child.

  • these methods must be defined with the same (or a less restricted) visibility.

Abstract Method's Visibility Child Method's Visibility
Public Public
Protected Protected or Public
  • the type hints and the number of required arguments must be the same.

For example, method prefixValue($prefix) has one argument, The method prefixValue in the child class should have the same arguments.

    public function prefixValue($prefix) {  }
  • child class may define optional arguments not in the parent's signature
    public function prefixName($name, $separator = ".") { }
  • A class can inherit only from one abstract class and and must override all its methods/properties that are declared to be abstract.

PHP Abstract class example

  1. The specification Phone describes the features of phones (what to do)
  2. The function of sending SMS is realized by the manufacturer. (How to do)
  3. object $myPhone is an actual phone in your hand.
  4. Use your phone to send a message.
  5. There are other manufacturers making cell phone. (child class)
<?php
abstract class Phone {
    abstract public function sendSMS($msg);
}

class ApplePhone extends Phone {
    public function sendSMS($msg) {
        return "Sending text: " . $msg;
    }
}

$myPhone = new ApplePhone();
echo $myPhone -> sendSMS("Hello Lautturi.");
?>
Date:2019-10-09 00:42:27 From:www.Lautturi.com author:Lautturi