note 128839 added to language.oop5.decon
| From: | HayleyWatson at main dot php dot net | Date: | Tue, 29 Aug 2023 03:11:19 +0000 |
| Subject: | note 128839 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220801@lists.php.net to get a copy of this message | ||
There are other advantages to using static factory methods to wrap object construction instead of
bare constructor calls.
As well as allowing for different methods to use in different scenarios, with more relevant names
both for the methods and the parameters and without the constructor having to handle different sets
of arguments of different types:
* You can do all your input validation before attempting to construct the object.
* The object itself can bypass that input validation when constructing new instances of its own
class, since you can ensure that it knows what it's doing.
* With input validation/preprocessing moved to the factory methods, the constructor itself can often
be reduced to "set these properties to these arguments", meaning the constructor promotion
syntax becomes more useful.
* Having been hidden away from users, the constructor's signature can be a bit uglier without
becoming a pain for them. Heh.
* Static methods can be lifted and passed around as first class closures, to be called in the normal
fashion wherever functions can be called, without the special "new" syntax.
* The factory method need not return a new instance of that exact class. It could return a
pre-existing instance that would do the same job as the new one would (especially useful in the case
of immutable "value type" objects by reducing duplication); or a simpler or more specific
subclass to do the job with less overhead than a more generic instance of the original class.
Returning a subclass means LSP still holds.
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 222.153.168.209)
----
Manual Page -- https://php.net/manual/en/language.oop5.decon.php
Edit -- https://main.php.net/note/edit/128839
Del: integrated -- https://main.php.net/note/delete/128839/integrated
Del: useless -- https://main.php.net/note/delete/128839/useless
Del: bad code -- https://main.php.net/note/delete/128839/bad+code
Del: spam -- https://main.php.net/note/delete/128839/spam
Del: non-english -- https://main.php.net/note/delete/128839/non-english
Del: in docs -- https://main.php.net/note/delete/128839/in+docs
Del: other reasons-- https://main.php.net/note/delete/128839
Reject -- https://main.php.net/note/reject/128839
Search -- https://main.php.net/manage/user-notes.php