0

There is any syntax to use something like this?:

<?php

function get_foo() {
    return new Foo();
}

get_foo()->foo_method();

?>
1
  • 2
    Have you tried this? It should work. Commented Aug 16, 2010 at 18:44

2 Answers 2

2

Using PHP 5.3 this works fine for me:

<?php

class Foo
{
    public function foo_method()
    {
        print 'hi';
    }
}

function get_foo()
{
    return new Foo();
}

get_foo()->foo_method();

prints hi

Stuff like this is used all over the place for database wrappers since you can do db()->query($sql) without any trouble.

Sign up to request clarification or add additional context in comments.

Comments

0

yeah PHP has this syntax, if a function returns an object, then you may call the objects property or method appended to the function's call exactly as it is in your question

Comments

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.