1

Is it possible to do this?

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);

I expect wooooo but I get "Use of undefined constant Bar".

0

1 Answer 1

2

You are missing '':

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');
Sign up to request clarification or add additional context in comments.

1 Comment

This is the right solution at the moment. But it is damn ugly to pass a classes name as string to a function... not only for type hinting and refactoring reasons. I want something like the Type from C# please :D . The Type-solution is just so smooth and opens up sooo many possibilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.