php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79666 Allow to further extend anonymous class
Submitted: 2020-06-02 12:58 UTC Modified: 2020-06-02 15:52 UTC
From: michael dot vorisek at email dot cz Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 7.4.6 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
24 + 48 = ?
Subscribe to this entry?

 
 [2020-06-02 12:58 UTC] michael dot vorisek at email dot cz
Description:
------------
This is a feature request to further improve AST and allow to extend class defined with variable and/or anonymous class. See example.

Instancing anonymous class is already possible.

Test script:
---------------
$cl = get_class(new class () extends \stdClass {
    public function test() {
        echo 'x';
    }
});

var_dump($cl);
$v = new $cl();
$v->test();

$clExtended = get_class(new class () extends $cl /* not possible in PHP 7.4 */ {
    public function test() {
        echo 'y';
    }
});

var_dump($clExtended);
$v = new $clExtended();
$v->test();

Expected result:
----------------
string(101) "class@anonymous�/mnt/www..."
x
string(101) "class@anonymous�/mnt/www..."
y

Actual result:
--------------
not possible in PHP 7.4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-02 13:05 UTC] michael dot vorisek at email dot cz
Parser needs to be fixed also for eval with anonymous string class name (probably everywhere, ie. to allow to use "class@" as literal and not as "class" kw):

$cl = get_class(new class () extends \stdClass {
    public function test() {
        echo 'x';
    }
});

var_dump($cl);
$v = new $cl();
$v->test();


eval('

$clExtended = get_class(new class () extends '.$cl.' {
    public function test() {
        echo "y";
    }
});

var_dump($clExtended);
$v = new $clExtended();
$v->test();

');
 [2020-06-02 15:52 UTC] nikic@php.net
-Summary: Allow to fursther extend anonymous class +Summary: Allow to further extend anonymous class -Status: Open +Status: Wont fix
 [2020-06-02 15:52 UTC] nikic@php.net
This is a "no" from my side. Of course, you or someone else may go through the RFC process to convince me to the contrary.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 15:01:29 2024 UTC