Functions and classes in PHP
A function gives a piece of work a name. A class bundles data and the work done with it. This text shows both in working code, and the point at which you switch from one to the other.
What you will learn
A function gives work a name
As soon as the same sequence with the same business meaning appears in two or three places, it belongs in a function. The gain is not the saved line, but that a change now happens in only one place. The gain is not the saved line, but that a change now happens in only one place.
declare(strict_types=1); function bruttoPreis(int $nettoCent, float $satz = 0.19): int { return (int) round($nettoCent * (1 + $satz)); } echo bruttoPreis(1000); // 1190 echo bruttoPreis(1000, 0.07); // 1070 echo bruttoPreis(nettoCent: 1000); // named argumentNote the amount in whole cents. This is no coincidence: floating-point numbers are unsuitable for money, and a function signature is the best place to make this decision visible.
A class holds together what belongs together
When several functions keep passing around the same values, those values and the functions belong together. That is exactly what a class is.
declare(strict_types=1);
final class Auftragsposition
{
public function __construct(
private readonly string $artikel,
private readonly int $menge,
private readonly int $einzelpreisCent,
) {
if ($menge < 1) {
throw new InvalidArgumentException('Menge muss mindestens 1 sein.');
}
}
public function gesamtCent(): int
{
return $this->menge * $this->einzelpreisCent;
}
public function beschreibung(): string
{
return sprintf('%dx %s', $this->menge, $this->artikel);
}
}
$pos = new Auftragsposition('Dichtung', 3, 450);
echo $pos->beschreibung(); // 3x Dichtung
echo $pos->gesamtCent(); // 1350Three things happen here that a loose function cannot do. The check in the constructor ensures that no position with a quantity of 0 can exist — no later caller has to check that any more. The data cannot be changed from outside. And the name says what it is about.
When a function becomes a class
Three signs that point towards a class; usually one is enough to seriously consider it:
The reverse case occurs just as often: a class with a single method and no properties is really a function. That is not a mistake, just unnecessary effort when reading.
An example from practice
What type declarations really do in operation
declare(strict_types=1);
function tageBis(DateTimeImmutable $ziel): int
{
$heute = new DateTimeImmutable('today');
return (int) $heute->diff($ziel)->format('%r%a');
}
// Correct:
echo tageBis(new DateTimeImmutable('2027-01-01'));
// Wrong — noticed IMMEDIATELY, not only in the result:
// echo tageBis('2027-01-01');
// TypeError: tageBis(): Argument #1 ($ziel) must be of type
// DateTimeImmutable, string givenThat is the real benefit: the error appears where it was made, not three levels further down. In a grown application that is the difference between five minutes and half a day.
Common misconceptions
Further sources
- PHP manual: Functions signatures, default values, named arguments
- PHP manual: Classes and objects the complete language section
- PHP manual: Type declarations what exactly strict_types changes
About the author
Fabian Lücker is managing partner of PHP-Schmiede KG in Rheine and has worked in software development since 2007. He is responsible for development and design, and writes here about decisions that come up in real projects.