You need to add actual functionality to the object to achieve this. Simply casting an array to an object only creates an object that holds some values, it is not very different from an array. There's no notion of "default values" for either arrays or objects, the only way to simulate this concept is by implementing it using magic methods, in this case __toString
. As such, you need to create a class akin to this:
class ObjectWithDefaultValue {
public function __construct($params) {
// assign params to properties
...
}
public function __toString() {
return $this->obj1;
}
}
function sth() {
$obj = new ObjectWithDefaultValue(array(
"obj1" => $obj1,
"obj2" => $obj2,
"obj3" => $obj3
));
return $obj;
}
$obj = sth();
echo $obj;