Summary: in this tutorial, you will learn how to use the PHP abstract class to define an interface for other classes to extend.
Introduction to the PHP abstract class
An abstract class is a class that cannot be instantiated. Typically, an abstract defines an interface for other classes to extend.
To define an abstract class, you add the abstract
keyword as follows:
<?php
abstract class className
{
// ...
}
Code language: HTML, XML (xml)
An abstract class can have properties and methods as a regular class. But it cannot be instantiated.
Similar to an abstract class, an abstract method is a method that does not have an implementation. To define an abstract method, you also use the abstract
keyword before the method signature like this
abstract function methodName(arguments);
Code language: PHP (php)
In most cases, an abstract class will contain at least one abstract method though it is not required. If a class contains one or more abstract methods, it must be an abstract class.
If a class extends an abstract class, it must implement all abstract methods or itself be declared abstract.
PHP abstract class example
The following example defines an abstract class called Dumper
that has an abstract method dump()
:
<?php
abstract class Dumper
{
abstract public function dump($data);
}
Code language: HTML, XML (xml)
To dump information on the web, you can extend the Dumper
class.
The following defines the WebDumper
class that extends the Dumper
class. Since the Dumper
class has the dump() abstract method, the WebDumper
class needs to implement the dump()
method:
<?php
abstract class Dumper
{
abstract public function dump($data);
}
class WebDumper extends Dumper
{
public function dump($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
}
}
Code language: HTML, XML (xml)
The following creates a new instance of the WebDumper
class and call the dump()
method:
$webDumper = new WebDumper();
$webDumper->dump('PHP abstract class is awesome!');
Code language: PHP (php)
If you open the web browser, you’ll see the message is wrapper inside a <pre> tag:
string(30) "PHP abstract class is awesome!"
Code language: JavaScript (javascript)
Later, if you want to dump the information to the command line, you can define a class e.g., ConsoleDumper
that extends the Dumper
class:
<?php
class ConsoleDumper extends Dumper
{
public function dump($data)
{
var_dump($data);
}
}
Code language: HTML, XML (xml)
The dump()
method of the ConsoleDumper
calls the var_dump()
function to dump the information to the command line.
This UML diagram illustrates the relationship between the Dumper, WebDumper, and ConsoleDumper classes:
The following defines a DumperFactory
class that has a static method getDumper()
.
The getDumper()
method returns a new WebDumper
object if the script is executed on the webserver and ConsoleDumper
object if the script is executed on the command line:
class DumperFactory
{
public static function getDumper()
{
return PHP_SAPI === 'cli'
? new ConsoleDumper()
: new WebDumper();
}
}
Code language: PHP (php)
The following uses the DumperFactory
to get a Dumper based on where the script runs and show a message:
$dumper = DumperFactory::getDumper();
$dumper->dump('PHP abstract class is awesome!');
Code language: PHP (php)
If you execute the script on the web browser, you’ll see the following message wrapped nicely in a <pre> tag:
string(30) "PHP abstract class is awesome!"
Code language: JavaScript (javascript)
However, if you run the script using PHP CLI, you’ll only see the message without the <pre>
tag.
Put it all together:
<?php
abstract class Dumper
{
abstract public function dump($data);
}
class WebDumper extends Dumper
{
public function dump($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
}
}
class ConsoleDumper extends Dumper
{
public function dump($data)
{
var_dump($data);
}
}
class DumperFactory
{
public static function getDumper()
{
return PHP_SAPI === 'cli'
? new ConsoleDumper()
: new WebDumper();
}
}
$dumper = DumperFactory::getDumper();
$dumper->dump('PHP abstract class is awesome!');
Code language: HTML, XML (xml)
Summary
- An abstract class cannot be instantiated. It provides an interface for other classes to extend.
- An abstract method doesn’t have an implementation. If a class contains one or more abstract methods, it must be an abstract class.
- A class that extends an abstract class needs to implement the abstract methods of the abstract class.