Quasiconstructor

From WoozleCodes
Jump to navigation Jump to search

"Quasiconstructor" (QC) is my word for the situation in PHP where instead of a standard __construct([ <...> ]) function, there's a static function which first calls that constructor (which usually has no arguments and is often protected so outside code can't accidentally invoke it) and then sets up the new (unprovisioned) object with what it has available.

Naming

  • QC names pretty much always start with "From", with the rest of the name referencing the type(s) of parameter(s) needed.
    • These are often paired with a protected "With" function of the same name, which does as much of the heavy-lifting as possible. The static function FromX(), ideally, just creates a blank object $oThis, calls $oThis->WithX(XType $arg), and then returns $oThis. In practice, it's usually more complicated.
  • 2026/07/22 convention: If the basic idea is to construct the object using $A but that means it will also need $B to provide missing information, I will not name the function something like "FromAAndB()" (nor even something clearer but more awkward like "From_A_and_B()") but rather just referencing the starter-info, $A -- like "FromA()".
    • This makes it easier to remember the QC's name (or what it should be named, if it hasn't yet been written) when I want to use it.
    • Also, the starter info should be the last argument.