Abstract Classes

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.

<?php
require_once('Mammal.php');

abstract class Dog extends Mammal
{
    //...

    abstract public function walk(): string;
    abstract public function bark(): void;
}
Running our example code above, we’ll get the following error.

The reason that we’re getting this is because we’ve implemented the bark() method inside of the GermanShepherd class, but we didn’t specify everything, primarily the return type of void.
<?php
require_once("Dog.php");

class GermanShepherd extends Dog
{
    //...

    public function bark(): void
    {
        echo "Loud Barking";

    }

    public function walk(): string {
        echo "I am a walking German Shepherd";
        return parent::walk();
    }
}
Moving up a level to our Mammal class, we can make a modification to the walk() method, since it is an abstract concept. We also need to make the modification to the class itself and declare it as abstract.

The walk() method is a good example to experiment with. Considering that the Mammal class now has an abstract declaration of the method, it can now be implemented within the abstract Dog class. Yes, you can implement those abstract methods within an abstract class as long as you’re inheriting the abstract declaration. You can also pass it along to the GermanShepherd class by adding an abstract declaration of your own.

If we remove the abstract method declaration from the Dog class and leave it in the Mammal class, it still works. The declaration is passed down to the GermanShepherd class through inheritance. The only time that we’ll get the error is if we remove the walk() method from the GermanShepherd class.

That’s really it. I think we covered most of the abstract concepts that you’re likely to come in contact with during your development career. We haven’t touched the Animal class since we’ll look at that in the next article.

Self Keyword

GAZING INTO THE MIRROR OF ‘SELF’ FOR CODE CLARITY

PHP – P59:SELF KEYWORD

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.

 

Abstract Classes

Building Worlds on Abstract Class Pillars

PHP – P60: abstract classes

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

PHP – P61: INTERFACE

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.

Leave a Reply