Building Worlds on Abstract Class Pillars
Abstract classes represent abstract concepts. In our previous article, we created various classes that inherited properties and methods from its parent.
https://www.dinocajic.com/php-self-keyword/
Looking at the Animal class hierarchy, the Animal class itself is an abstract concept. We can vaguely say Animal, but what type of animal? A Dog may be a narrow enough concept, but a GermanShepherd is definitely a concrete class, not abstract.
Abstract classes are classes that start with the word abstract. If a class contains at least one abstract method, it must be declared as abstract. If a method inside of an abstract class is defined as abstract, you can only declare the method signature. There can be no implementation of the method itself.
All methods that are marked as abstract must be implemented in the child class during inheritance. You’ll be required to copy the method signature as is such as the visibility modifier, signature, etc.
Review: Take a look at the following classes. These are the ones that we’ll be modifying.
Let’s create a new file and bring in our GermanShepherd class for testing.
<?php
// Abstract Classes
require_once("GermanShepherd.php");
$gs_dog = new GermanShepherd("Brown", "Oct 10, 2015", "Brown");
$gs_dog->bark();
We know that this works without any type of modification. Run it on your computer to see the result. We can now start with the modifications.
Our Dog class represents an abstract concept. Let’s change it to an abstract class.
<?php
require_once('Mammal.php');
abstract class Dog extends Mammal
{
const HAS_TAIL = true;
protected $does_shed = true;
protected $breed;
protected $fur_color;
//...
}
We prepended the class declaration with the abstract keyword. Why did we declare this class as an abstract class? Because we want to make sure that it can never be instantiated. Go ahead and try now. You will get an error. The Dog class only needs to be inherited, never instantiated.
Reviewing the code so far,
- Our GermanShepherd class is a concrete class.
- The Dog class is now an abstract class.
- Currently, there are no abstract methods within the Dog class.
Running the example code above should give you the exact same results.
Moving one step further, lets modify a couple of the methods to be abstract methods within the Dog class: bark() and walk(). Remember that our abstract methods can only be declarations, so we’ll need to remove the content between the curly braces. You can declare what type of value will be returned.
GAZING INTO THE MIRROR OF ‘SELF’ FOR CODE CLARITY
For static class members, we don’t use the $this keyword, we use self. That and the scope resolution operator allows us to access the static properties and methods.
Building Worlds on Abstract Class Pillars
Abstract classes are classes that start with the word abstract. If a class contains at least one abstract method, it must be declared as abstract. If a method inside of an abstract class is defined as abstract, you can only declare the method signature.
DESIGNING ELEGANT SOLUTIONS THROUGH INTERFACE MAGIC
The topic of interfaces. An interface resembles an abstract class but there are a few differences. With an interface, you can only define the functionality.