php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71462 Anonymous classes and external variables
Submitted: 2016-01-27 11:32 UTC Modified: 2017-08-04 23:57 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: iletaitunefoisfenghuang at gmail dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: Next Minor Version OS: Any
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-01-27 11:32 UTC] 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 ?


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-27 11:52 UTC] 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 14:43 UTC] 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-02-01 06:26 UTC] stas@php.net
-Package: PHP Language Specification +Package: *General Issues
 [2017-08-04 23:57 UTC] stas@php.net
-Status: Open +Status: Suspended
 [2017-08-04 23:57 UTC] stas@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 00:01:28 2024 UTC