Hello internals, I've just occured a syntax problem in the following script:
<?php class C { public $n = 1; } $o = new C(); $o->f = function () use ($o) { echo $o->n; }; $o->f(); ?> The result of this script is "Fatal Error: Call to undefined method C::f()". I don't know this is the expected result. After trying more tests of adding/removing properties on the fly, I concluded these inconsistencies/flaws: 1. There is no way to add/remove instance-level members (both properties and methods) to class dynamically, but a way to add them to instance itself, which is a little buggy as above codes turns out; 2. There is no way to add/remove static members dynamically either; 3. There are __get(), __set(), __call() for instance-level members, and __callStatic() for static methods, but lacks __getStatic() and __setStatic() for static properties; 4. While using static class as object (general concept of "object", not "instance" here), it's extremly complex to simulate "prototype object", as static members simply do'not duplicate themselves at all while inheriting, therefore all of the child classes share a single static member of the parent class; 5. The inheritance rule of static member is not well documented, developers has to try it out themselves; 6. Static methods are allowed in interfaces, but not allowed in abstract class, which breaks the rule of abstraction; 7. An interface which has only static methods cannot ensure static methods in a class which implements it. Sorry to raise so many complaint, but these inconsistencies bring me a big headache when developing. I would like to hear the design rules of PHP5's object model, at least, the explanations of the above inconsistencies. Thanks very much! -- Best regards, Jingcheng Zhang P.R.China