Now that PHP8.4 support new MyClass()->method() without parentheses does it still make sense to add or have empty named constructor like MyClass::new() or MyClass::make() or MyClass::create()
[#]php84 #apidesign
=> More informations about this toot | More toots from nyamsprod@phpc.social
@nyamsprod I've never been a fan of static constructor methods for any reason. Like that's the entire purpose of __construct
and circumventing it feels ... off. Plus it's more often used to hide or enable singleton state that gives it an odor.
There's the argument about validating the shape/type/value or having convenience conversion methods (e.g. Vec2::from(Vec3)
) but that friction should be an indicator of doing something wrong. Like it should be the Vec3 owns that logic.
¯_(ツ)_/¯ 2¢
=> More informations about this toot | More toots from zimzat@mastodon.social
@zimzat
named constructors is PHP equivalent to method Overloading. I always take the example of a Geolocation value object class there is no single way to define geolocation so have the default constructor private and multiple named constructors on the same object sound logic/reasonable to me. But that's not the feature we are discussing here today
=> More informations about this toot | More toots from nyamsprod@phpc.social
@nyamsprod Right, so then it depends on what the purpose of new/make/create is. "It depends"? 🤷♂️
Are methods called and then the instance discarded? Should use Dependency Injection instead.
Purely the syntactic sugar over (new X())->y()
?
Is it a DTO with setters and a fluent interface?
With the Geolocation example there should still be one right way to make the object. E.g. public function __construct(public readonly float $longitude, public readonly float $latitude)
.
=> More informations about this toot | More toots from zimzat@mastodon.social
@nyamsprod It never made sense. It's funny though how innocent "still" can change simple yes or no question into loaded nightmare.
=> More informations about this toot | More toots from shudder@phpc.social
@nyamsprod Well if you had them as a syntactic sugar then no.
If you had them because there's lets say a factory method there due to some hierarchy then yes. Could be other reason too but if the only thing you were trying to avoid is putting the parenthesis, well you shouldn't have them there in the first place :D Can just put the parenthesis..
=> More informations about this toot | More toots from thgs@phpc.social This content has been proxied by September (3851b).Proxy Information
text/gemini