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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2020 The PHP Group
All rights reserved.
Last updated: Sun Nov 29 13:01:23 2020 UTC