Named constructors
Use named static constructors (aka "static factories") to encapsulate logic and data and create valid entities:
PHP
// GOOD
// 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: have robust API that do not allow developers to create objects with invalid state (e.g. missing parameter/dependency). A great video on this topic: Marco Pivetta «Extremely defensive PHP»
