Php Closing an object, how to read it?

I have this code, but I'm stuck ...

$my_var = function (){ return array('hello you'); }; var_dump($my_var); // returns object(Closure)#2 (0) { } 

how do i echo $my_var ?

I would suggest that it would be echo $my_var[0] ; but it does not work.

Fatal error: Cannot use object of type Closure as array in ...

+4
source share
3 answers

Closing is a function. Therefore, you should call it, for example:

 $myvar(); 

Since php5.4 with array access:

  echo $myvar()[0]; 
+7
source

$ my_var is a function. You must first call it to get the return value.

+1
source

try print_r, it will print an array or object

 print_r($my_var); 
-1
source

Source: https://habr.com/ru/post/1439530/


All Articles