Req #71462 [Opn->Sus]: Anonymous classes and external variables

From: Date: Fri, 04 Aug 2017 23:57:56 +0000
Subject: Req #71462 [Opn->Sus]: Anonymous classes and external variables
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210501@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71462&edit=1 ID: 71462 Updated by: stas@php.net Reported by: iletaitunefoisfenghuang at gmail dot com Summary: Anonymous classes and external variables -Status: Open +Status: Suspended Type: Feature/Change Request Package: *General Issues Operating System: Any PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: Thank you for your interest in PHP and for submitting a feature request. Please be aware that due to the magnitude of change this request requires, it would be necessary to discuss it on PHP Internals list (internals@lists.php.net) as an RFC. Please read the guide about creating RFCs here: https://wiki.php.net/rfc/howto If you haven't had experience with writing RFCs before, it is advised to seek guidance on the Internals list (http://php.net/mailing-lists.php) and/or solicit help from one of the experienced developers. Please to not consider this comment as a negative view on the merits of your proposal - every proposal which requires changes of certain magnitude, even the very successful and widely supported ones, must be done through the RFC process. This helps make the process predictable, transparent and accessible to all developers. Previous Comments: ------------------------------------------------------------------------ [2016-01-27 14:43:49] iletaitunefoisfenghuang at gmail dot com Well, as usual (for me), I have no answer from the bot when I send the confirmation ( [confirm: ID <myemail>] ) to confirm-sender@lists.php.net (I've waited about an hour now)... So, anyone can manually add me ? ------------------------------------------------------------------------ [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

« previous php.bugs (#210501) next »