rfc:object-initializer
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:object-initializer [2019/09/16 13:48] – added note about constants brzuchal | rfc:object-initializer [2019/10/07 11:14] – removed note about accepting the patch cause there is no proposed implementation yet brzuchal | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 1.0 | * Version: 1.0 | ||
* Date: 2019-09-03 | * Date: 2019-09-03 | ||
- | * Author: Michał Brzuchalski <michal.brzuchalski@gmail.com> | + | * Author: Michał Brzuchalski <brzuchal@php.net> |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
- | |||
- | |||
===== Introduction ===== | ===== Introduction ===== | ||
Line 82: | Line 80: | ||
The main difference is that object initializers allow creating a new object, with its assigned properties in a single expression. For eg. factory methods where normally a significant amount of argument has default values or simple Data Transfer Objects could benefit. | The main difference is that object initializers allow creating a new object, with its assigned properties in a single expression. For eg. factory methods where normally a significant amount of argument has default values or simple Data Transfer Objects could benefit. | ||
- | > Note! Currently, language allows instantiating object and initializing only a subset of typed non-nullable properties without a default value. These rules apply to object initializer the same way, meaning the creation of properly initialized object state is in authors responsibility, | + | > **Note!** Currently, language allows instantiating object and initializing only a subset of typed non-nullable properties without a default value. These rules apply to object initializer the same way, meaning the creation of properly initialized object state is in authors responsibility, |
==== Restrictions ==== | ==== Restrictions ==== | ||
Line 108: | Line 106: | ||
Due to the fact that objects in PHP simply have constructor directly declared in class definition or indirectly through the defaulting constructor, | Due to the fact that objects in PHP simply have constructor directly declared in class definition or indirectly through the defaulting constructor, | ||
- | > Note! Object instantiation allows only constructors without required arguments to be used. Any class which requires passing arguments to constructor cannot be used in combination with object initializer. | + | > **Note!** Object instantiation allows only constructors without required arguments to be used. Any class which requires passing arguments to constructor cannot be used in combination with object initializer. |
Line 134: | Line 132: | ||
- | > Note! Classes without constructor desired to mimick " | + | > **Note!** Classes without constructor desired to mimick " |
- | > Note! If a class needs validation upon to validate its invariants a proper validation logic needs to be called after initialization. To combine it with object initializer and keep the validation process encapsulated, | + | > **Note!** If a class needs validation upon to validate its invariants a proper validation logic needs to be called after initialization. To combine it with object initializer and keep the validation process encapsulated, |
==== Lexical scope ==== | ==== Lexical scope ==== | ||
Line 307: | Line 305: | ||
As this is a language change, a 2/3 majority is required. | As this is a language change, a 2/3 majority is required. | ||
- | The vote is a straight Yes/No vote for accepting the RFC and merging the patch. | + | The vote is a straight Yes/No vote for accepting the RFC. |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | . | ||
+ | <doodle title=" | ||
+ | * = | ||
+ | * => | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== |
rfc/object-initializer.txt · Last modified: 2019/10/24 07:36 by brzuchal