[PHP-BUG] Req #71462 [NEW]: Anonymous classes and external variables

[email protected] ("iletaitunefoisfenghuang at gmail dot com")
Newsgroups php.standards
Message-ID <[email protected]>
From:             iletaitunefoisfenghuang at gmail dot com
Operating system: Any
PHP version:      Next Minor Version
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:Anonymous classes and external variables

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 bug report at https://bugs.php.net/bug.php?id=71462&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71462&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71462&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71462&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71462&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71462&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71462&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71462&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71462&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71462&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71462&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71462&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71462&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71462&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71462&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71462&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71462&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71462&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71462&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71462&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71462&r=mysqlcfg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.