Php

Named constructors

Use named static constructors (aka "static factories") to encapsulate logic and data and create valid entities:

// Models\Team.php
public static function createFromSignup(AlmostMember $almostMember): Team
{
    $team = new Team();
    $team->name = $almostMember->company_name;
    $team->country = $almostMember->country;
    $team->save();

    return $team;
}

Reason: a robust API that does not allow developers to create objects in an invalid state (e.g. a missing parameter or dependency). A great video on this topic: Marco Pivetta — «Extremely defensive PHP»

Copyright © 2026