如何从PHP中的函数返回哈希对象?

时间:2018-03-16 14:18:26

标签: php function object curl

我想做一些我确信很简单的事情,但我无法从文档中收集到如何做到这一点。

我想做的就是:

function something() {
 // Do some stuff with cURL
 set $object->name;
 set $object->corporation;
 // some if statement
 set $object->other; //if cURL finds "other" exists

 return $object;
}

能够做到这一点:

$result = something();
$name = $result->name;
etc.

有谁知道最好的方法吗?我使用程序风格,对物体知之甚少,因此我正在努力学习。

1 个答案:

答案 0 :(得分:6)

function foo(){
    $result = new stdClass();
    $result->name = "Joe";
    $result->other = "bar";
    return $result;
}
$object = foo();
echo $object->name; // Should display "Joe"