Skip to content

Small business web,
explained since the early 2010s

7eye / Blog / Three techniques for better PHP classesPHP

Three techniques for better PHP classes

Modern PHP versions make classes shorter and safer to work with.

Section
PHP
Reading time
3 min
Written by
7eye editors
Format
Article

A / 01Typed properties

Declare property types so mistakes surface early. Typed properties are available from PHP 7.4.

B / 02Constructor property promotion

From PHP 8.0, you can declare and assign properties directly in the constructor signature, removing repetitive code.

C / 03Program to interfaces

Define interfaces for important behaviour and depend on them, not on concrete classes. It makes testing and swapping implementations easier. See the PHP manual on classes and objects.

Read next

  1. 001Programming languages used in web developmentDevelopment3 min
  2. 002Where to learn programming for the webLearning3 min
  3. 003Xdebug: debugging PHP properlyPHP3 min