Skip to content

Latest commit

 

History

History
140 lines (104 loc) · 3.77 KB

6.md

File metadata and controls

140 lines (104 loc) · 3.77 KB

Lesson 6: Abstraction

New Keywords:

  • abstract
  • final

"An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries relative to the perspective of the viewer."

Abstract Classes

An abstract class cannot be instantiated, but rather, child classes can be extended from the abstract class.

Abstract classes are typically used as a means to organize a project. You can't create an object from an abstract class. Instead, a child class extends an abstract class, and then an object can be instantiated from the child class.

The child class must implement all of the abstract methods listed in the parent class. PHP doesn't use abstract properties, only methods, so you can signify an abstract property, but PHP doesn't force you to use it in the implementing classes.

A class can be declared as abstract by using the abstract keyword before class, and any abstract methods must also use the abstract keyword.

Private methods cannot be declared as abstract, only public or protected methods.

Example:

<?php

abstract class ContentEntityBase {
  // ... Other properties and methods.
  
  public function id() {
    return $this->getEntityKey('id');
  }
}

class Node extends ContentEntityBase {
  // ... Other properties and methods.
}

Abstract classes can have concrete methods defined. This allows for classes that only partially need to be implemented by a derived class. In the example above, the id concrete method will be available to classes that extend the ContentEntityBase class.

If you fail to implement all of the abstract methods outlined in the abstract class in a child concrete class, PHP will throw an error.

Remember that abstract methods are only declarations, and that concrete methods must contain a function body whereas abstract methods do not.

PHP will throw an error if:

  • a method in the abstract class isn't implemented in a derived class.
  • an implemented method doesn't abide by its signature (parameters)
  • the method is of a different visibility than specified

We can extend an abstract into a concrete class by using the extends keyword:

<?php

abstract class Query {
  // ... Other properties and methods.
  
  /**
   * Runs the query against the database.
   */
  abstract protected function execute();
  
  /**
   * The toString operation is how we compile a query object to a prepared statement.
   */
  abstract public function __toString();
  
  /**
   * Returns a unique identifier for this object.
   */
  public function uniqueIdentifier() {
    return $this->uniqueIdentifier;
  }
}

class Select extends Query {
  // ... Other properties and methods.
  
  /**
   * {@inheritdoc}
   */
  public function execute() {
    if (!$this->preExecute()) {
      return NULL;
    }

    $args = $this->getArguments();
    return $this->connection->query((string) $this, $args, $this->queryOptions);
  }
  
  /**
   * {@inheritdoc}
   */
  public function __toString() {
    if (!$this->compiled()) {
      $this->compile($this->connection, $this);
    }
    
    // SELECT
    $query = 'SELECT ';
    if ($this->distinct) {
      $query .= 'DISTINCT ';
    }
    
    // More processing.
    return $query;
  }
}

final

The final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Exercise:

  • Convert the Person class into an abstract class.
  • Convert into concrete classes the Developer and Designer classes.
  • Instantiate objects from the concrete classes.
  • Call some methods on those objects to see how they behave.
  • Try instantiating an object of the abstract class to look at the PHP error.