Wiliam
Wiliam

Reputation: 3754

Call an object function returned by a method within php

There is any syntax to use something like this?:

<?php

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

get_foo()->foo_method();

?>

Upvotes: 0

Views: 179

Answers (2)

ovais.tariq
ovais.tariq

Reputation: 2625

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

Upvotes: 0

Xeoncross
Xeoncross

Reputation: 57184

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.

Upvotes: 2

Related Questions