rfc:class_properties_initialization
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:class_properties_initialization [2010/07/30 10:23] – k.antczak | rfc:class_properties_initialization [2010/07/30 10:47] – k.antczak | ||
---|---|---|---|
Line 5: | Line 5: | ||
* Status: In Draft | * Status: In Draft | ||
* First Published at: http:// | * First Published at: http:// | ||
- | * Related RFC: [[rfc: | + | * Related RFC: [[rfc: |
===== Introduction ===== | ===== Introduction ===== | ||
Line 13: | Line 13: | ||
==== Why do we need it? ==== | ==== Why do we need it? ==== | ||
- | * **objects structures** - structured objects initialization | + | * **code readability** |
===== Proposal ===== | ===== Proposal ===== | ||
+ | |||
+ | **Main idea** | ||
+ | |||
+ | <code php> | ||
+ | class Foo | ||
+ | { | ||
+ | private $var1 = function () { /* some callback ? */ } | ||
+ | |||
+ | private $var2 = array ( ' | ||
+ | |||
+ | private httpRequest $var3 = NULL; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | instead of: | ||
+ | |||
+ | <code php> | ||
+ | class Foo | ||
+ | { | ||
+ | private $var1; | ||
+ | |||
+ | private $var2; | ||
+ | |||
+ | private $var3 = NULL; | ||
+ | |||
+ | public function __construct () | ||
+ | { | ||
+ | $this -> var1 = function () { /* some callback ? */ } | ||
+ | | ||
+ | $this -> var2 = array ( ' | ||
+ | |||
+ | if ( ( $request = SomeCore :: getRequest () ) instanceof httpRequest ) | ||
+ | { | ||
+ | $this -> var3 = $request; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Other / Related** | ||
+ | |||
+ | * objects structures - structured objects initialization - like IoC/DI containers, where structure is not a result of code execution, but it's a result of strictly controlled design. | ||
<code php> | <code php> | ||
Line 21: | Line 63: | ||
{ | { | ||
private $var1 = new Bar(); | private $var1 = new Bar(); | ||
+ | |||
private $var2 = Bar :: getInstance (); | private $var2 = Bar :: getInstance (); | ||
+ | } | ||
+ | </ | ||
- | private $var3 = time (); | + | * initialization with functions |
- | + | ||
- | private $var4 = function () { /* some callback ? */ } | + | |
- | | + | <code php> |
- | + | class Foo | |
- | | + | { |
- | private httpRequest $var7 = getRequest (); | + | |
+ | |||
+ | private httpRequest $var2 = getRequest (); | ||
} | } | ||
</ | </ |
rfc/class_properties_initialization.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1