开发者

Call an object function returned by a method within php

开发者 https://www.devze.com 2023-01-12 06:59 出处:网络
There is any syntax to use somethi开发者_如何学Cng like this?: <?php function get_foo() { return new Foo();

There is any syntax to use somethi开发者_如何学Cng like this?:

<?php

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

get_foo()->foo_method();

?>


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消