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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: michael dot vorisek at email dot cz
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 10:01:33 2025 UTC