3 ms·
The idea behind it was to avoid having every typed property be nullable. It's important to note that the undefined state of a property cannot be read as a value
by TazeTSchnitzel 7y ago
The idea behind it was to avoid having every typed property be nullable. It's important to note that the undefined state of a property cannot be read as a value, and so can't be propagated anywhere.
- donatj 7y agoJust 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.