PHP类作为参数

时间:2014-10-07 14:22:12

标签: php class static arguments

是否可以这样做?

class Foo
{
    public function __construct($bar)
    {
        $barish = new $bar();
        $barish->woo();
    }
}

class Bar
{
    function woo()
    {
        echo "wooooo";
        die();
    }
}

// Here the magic should happen
$foo = new Foo(Bar);

我希望wooooo但是我得到"使用未定义的常量Bar"。

1 个答案:

答案 0 :(得分:2)

您遗失''

class Foo
{
    public function __construct($bar)
    {
        $barish = new $bar();
        $barish->woo();
    }
}

class Bar
{
    function woo()
    {
        echo "wooooo";
        die();
    }
}

// Here the magic should happen
$foo = new Foo('Bar');