Variable variable class extensions in PHP--is it possible?

前端 未结 8 1905
囚心锁ツ
囚心锁ツ 2020-12-06 14:22

Is something like the following possible in PHP?

$blah = \'foo1\';

class foo2 extends $blah {
    //...
}

class foo1 {
    //...
}

This g

相关标签:
8条回答
  • 2020-12-06 15:10

    I know this question was asked a long time ago, but the answer is relatively simple.

    Assuming you want to extend class foo if class foo exists, or class bar if it doesn't, you'd use:

    if(!class_exists('foo')) {
        class foo extends bar {
            function __construct() {
                parent::__construct();
            }
        }
    }
    
    class myclass extends foo{
        //YOUR CLASS HERE
    }
    
    0 讨论(0)
  • 2020-12-06 15:12

    you should have tried $$

    $blah = 'foo1';
    class foo2 extends $$blah {
        //...
    }
    
    class foo1 {
        //...
    }
    
    0 讨论(0)
提交回复
热议问题