-1

I have a function which returns an array:

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

And here is the usage:

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

See? I have to get the status of myfunc() like this myfunc()['status']. All I'm trying to do is getting it like this myfunc()->status. Any idea how can I modify the function to get the status like an object?

Martin AJ
  • 5,689
  • 5
  • 42
  • 92
  • 2
    Possible duplicate of [How to convert an array to object in PHP?](https://stackoverflow.com/questions/1869091/how-to-convert-an-array-to-object-in-php) – Ethan Jul 27 '18 at 08:47
  • You can either create a class that has the `status` field, or only cast your array to `object`, like `$obj = (object)['status' => true];` – AnthonyB Jul 27 '18 at 08:50

1 Answers1

2

Just cast the array into an object:

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

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

Dan
  • 4,598
  • 2
  • 11
  • 28