Req #71462 [Com]: Anonymous classes and external variables
[email protected] ("iletaitunefoisfenghuang at gmail dot com")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=71462&edit=1 ID: 71462 Comment by: iletaitunefoisfenghuang at gmail dot com Reported by: iletaitunefoisfenghuang at gmail dot com Summary: Anonymous classes and external variables Status: Open Type: Feature/Change Request Package: PHP Language Specification Operating System: Any PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: Well, as usual (for me), I have no answer from the bot when I send the confirmation ( [confirm: ID <myemail>] ) to [email protected] (I've waited about an hour now)... So, anyone can manually add me ? Previous Comments: ------------------------------------------------------------------------ [2016-01-27 11:52:05] rowan dot collins at gmail dot com Hi, this sounds like an interesting line of discussion. The best place to discuss complex / language-changing feature requests like this is probably on the php-internals mailing list, which you can subscribe to here: http://php.net/mailing-lists.php ------------------------------------------------------------------------ [2016-01-27 11:32:08] iletaitunefoisfenghuang at gmail dot com Description: ------------ An example worth a thousand words : Let's say we have a TableProxy class, with a custom __construct that do stuff around an inner protected static $columns. We want to create an anonymous class based on the abstract class tableProxy, with computed columns, for a fast and dummy script. We cannot do that because : - We cannot override the ctor with a different prototype to pass our external variable - We cannot set the variable after the creation of the anonymous class because we must use the "new" keyword therefore an instance of the anonymous class is created and so the constructor is triggered. Plus if the variable is not public, we have to define a setter in the anonymous class. The only way we can do this, is by creating a first anonymous class : $table = new class($fileBodyName) extends TableProxy {}; Then use reflection on that very anonymous class to set it up without ctor, and then call it manually : $r = new \ReflectionClass($table); $t2 = $r->newInstanceWithoutConstructor(); $t2::$columns = $computedColumns; $t2->__construct($param); So, as we see, there is a way. However we can all admit it's not the easiest way ever. What if we enhance the anonymous class feature to be able to use external variables with inner properties : $anon = new class() use($yolo) extends TableProxy { private $name = $yolo; }; It would be more efficient and save a lot of time. One could say "you just have to think about your definition of your primary class" . Yes, maybe, however I think the intent of anynomous classes is also to allow some freedom, to do stuff that is not inside the drawn lines. What do you think of it, does it worth a RFC or do I miss something and my proposal does not make sense ? ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71462&edit=1