【问题标题】:How can a function return an object?函数如何返回对象?
【发布时间】:2018-07-27 08:46:33
【问题描述】:

我有一个返回数组的函数:

function myfunc(){
    return ["status" => true];
}

这是用法:

if ( myfunc()['status'] ){
    // do something
}

看到了吗?我必须像这样myfunc()['status'] 获得myfunc() 的状态。我想做的就是像这样myfunc()->status。知道如何修改函数以获取对象的状态吗?

【问题讨论】:

标签: php function object


【解决方案1】:

只需将数组转换为对象:

function myFunc() {
    return (object) ['status' => true];
}

http://php.net/manual/en/language.types.object.php

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 2019-10-13
    • 1970-01-01
    • 2021-01-30
    • 2020-04-23
    • 2012-02-29
    • 2019-06-11
    • 1970-01-01
    • 2020-07-10
    相关资源
    最近更新 更多