How the array is actually converted into an object and what's the purpose/benefit/necessity of doing it?

32 Views Asked by At

I've a following already pre-written program in PHP which converts an array into object :

<?php
  $obj = (object) array('1' => 'foo');
  var_dump(isset($obj->{'1'})); // outputs 'bool(false)'
  var_dump(key($obj)); // outputs 'int(1)'
?>

With the above program I've few queries. I searched for their solution but couldn't find the authoritative answers to them.

Following are my queries:

  • The array is getting converted into object means what actually does happen? Isn't the declared array act as an object? Why it is done? Is there any specific name defined for such kind of conversion/casting?
  • Please explain some lines from the above code in a simple and lucid language. I'm not understanding what does the code line $obj->{'1'} mean?
  • After executing the code var_dump(key($obj)); why I'm getting 1 and not 0 since there is only one array element present in the array?

Thanks in advance.

0

There are 0 best solutions below