2 ms·
Just do it like TypeScript though where they just have to be defined by the end of the constructor or be nullable/have a default. A new state was not necessary
by donatj 7y ago
Just do it like TypeScript though where they just have to be defined by the end of the constructor or be nullable/have a default.
A new state was not necessary.
- TazeTSchnitzel 7y agoI think the problem is that doesn't work for cases where you have a minimal constructor and do the rest in a static method which wraps it, or where you construct the class while skipping the constructor (and probably some other cases). PHP allows more flexibility here than other languages, and these sorts of cases are not uncommon. Mocking libraries and databases interfaces do the latter, and using the former to simulate multiple constructors is a common pattern.
- fetbaffe 7y agoYes, a new state is unfortunate, however a lot of PHP code uses classes as pure data classes without any constructor & with only public properties, i.e the class functions more like a struct where you assign property values from outside the object instance. Requiring a constructor would remove a perfectly valid code pattern that is in use today.