Define a PHP Abstract Class with a Shared Method and Required Child Method
Learn to share a PHP method through an abstract class while requiring each child to supply the value that method prints.
If related PHP classes should print a value in the same way but supply different values, put the printing method in an abstract parent and make the value-producing method abstract. Each concrete child implements that required method, then inherits the working printing method. The result is one place for the shared behavior without forcing every child to return the same thing.
Declare the shared method and the abstract method
An abstract class is declared with abstract class. You cannot create a Label object directly; the class is meant to be extended. An abstract method declares what a child must provide, including its signature and visibility, but has no body in the parent. The PHP manual’s abstract-class rules also require a concrete child to define its inherited abstract methods with compatible signatures.
In this example, getValue(): string is the requirement. It is protected because printOut() needs to call it, while callers only need the public printing method. printOut(): void is the shared implementation: it gets a value and prints that value followed by a newline. The parent does not have to know which child supplied the string. This is the useful distinction between the two declarations: one specifies work that remains to be done, and the other already does work.
Extend the class and implement the required method
Here is the complete example. Each named child uses extends Label and provides a protected getValue(): string implementation. The two implementations return different strings, but neither child repeats printOut().
<?php
abstract class Label
{
abstract protected function getValue(): string;
public function printOut(): void
{
print $this->getValue() . "\n";
}
}
class DraftLabel extends Label
{
protected function getValue(): string
{
return 'Draft';
}
}
class PublishedLabel extends Label
{
protected function getValue(): string
{
return 'Published';
}
}
$draft = new DraftLabel();
$published = new PublishedLabel();
$draft->printOut();
$published->printOut();
The file includes its PHP opening tag and all the class definitions needed for this example. Notice that the parent’s abstract declaration ends with a semicolon rather than a method body. The child methods have bodies and keep the required return type. printOut() is public, so the calls at the bottom use it directly; getValue() remains an implementation detail of each child.
The call inside printOut() is the connection between the two halves of the pattern. For a DraftLabel, it gets 'Draft'; for a PublishedLabel, it gets 'Published'. The printing code itself stays unchanged. PHP’s abstract-class example uses the same arrangement: a common printOut() method calls $this->getValue().
Use the children and choose the pattern
The calls in the example produce:
Draft
Published
Check both lines, not just whether both method calls succeed. They show that the inherited method is shared while the value it receives depends on the child object. If a third concrete label is added, it needs its own compatible getValue() implementation; it does not need another copy of the printing method. A child that leaves the inherited abstract method unimplemented must itself remain abstract rather than being used as a concrete label.
Choose this shape when the common implementation matters. Here, every child uses the same rule for printing, including the newline, so keeping that rule in Label has a purpose. If all you need is a promise that separate classes offer a method, and there is no parent implementation to share, this particular abstract parent would not earn its place. The decision is less about the word abstract and more about whether printOut() contains behavior the children should actually inherit.
