Re: [RFC] Alternative typehinting syntax for accessors
| From: | Steve Clay | Date: | Sun, 06 Jan 2013 01:43:44 +0000 |
| Subject: | Re: [RFC] Alternative typehinting syntax for accessors | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-64591@lists.php.net to get a copy of this message | ||
On 1/5/13 7:25 PM, Anthony Ferrara wrote:
No, the other initializer that would be useful isI think property init() would enable two bad practices: 1. putting too much logic in a property (SRP) 2. building dependencies within the object instead of injecting them (the better way is what we already do: receive dependencies in the constructor and assign them to the properties)new Foo. Assigning a default value is not an initializer. An initializer is something that's run on object construction (and has been discussed in other threads): class Foo {public DateTime $bar { init: { $bar = new DateTime(); } }}
null pointers. And if you set the property in the constructor (and it's typed), you should never have the chance for it to be non-null, unless you explicitly define it that way. Otherwise, you'd have to litter your code with is_null checks... Which defeats the point of the syntax in the firstWhich is why I think the default setting signature should not have "= null". If someone wants to allow setting the property *back* to null, they could easily: public Foo $foo { get; set($value = null); } I agree with you that, in an ideal world, a property with a declared type would *never not have* that type, but I think a property init() would be a bad tradeoff to make that a reality. The class constructor is the best place to do this, and maybe this is why C# forces property storage to be in fields; you can set fields directly from the constructor without going through property setters. Steve Clay -- http://www.mrclay.org/