Thanks Rowan for the detailed explanation. I thought that leveraging `__get` / `__set` internally would have addressed many of the type checking issues that you mentioned. I've used __get/__set in userland to enforce property types and hoped that an internal C based solution would something that is reasonable to code and implement.
Also, can anyone in internals respond to this inquiry? https://github.com/php/php-src/pull/1797#issuecomment-394434391 On Wed, Jun 6, 2018 at 4:00 AM Rowan Collins <rowan.coll...@gmail.com> wrote: > On 6 June 2018 at 07:14, Ryan Jentzsch <ryan.jentz...@gmail.com> wrote: > >> Why would something like this not work? >> >> strict class MyClass >> { >> protected int $foo = 1; >> public string $bar = "strict keyword in front of class >> allows/enforces strict properties"; >> private string $isItReallyThatDifficult = "to implement this?"; >> } >> > > > The problem is not in defining what code has type annotations, it's what > to do with them afterwards. > > The performance hit comes from the fact that *every* time you run: > > $foo->bar = $baz; > > The engine now needs to check whether $foo is a class with type > constraints, and what the type constraint of bar is, and whether the type > of $baz matches that constraint. The answer will be "no constraint" most of > the time, but it doesn't know that until it checks, at run-time, every time. > > Nor is it just straight-forward assignments that would need to change, > there are knock-on effects across the language, such as assignment by > reference: > > strict class MyClass > { > public int $foo; > public string $bar; > } > > $a = new MyClass; > $inst =& $a->foo; // If this is allowed... > $inst = 'bad'; // ...then this needs to error > > $s = 'good'; > $a->bar =& $s; // And if this is allowed... > $s = 42; // ... then this needs to error > > The previous proposal for typed properties simply raised errors when you > tried to use reference assignment with any typed property, which feels like > an arbitrary restriction to me. The alternative is to allow *every variable > in the language* to carry type constraint information. I mused some more on > this here: http://rwec.co.uk/q/php-type-system > > In short, yes, it really is that difficult. > > Regards, > -- > Rowan Collins > [IMSoP] >