Samuel
Samuel

Reputation: 21

PHP access object member as if array member

Is it possible to access to object member as if array member?

for example

echo $a->b;

to

echo $a[b];

Would it be any php ini settings, or this is quite impossible in PHP?

Upvotes: 1

Views: 51

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

I tried for a one-liner but:

$x = (array)$a;
echo $x['b'];

Or of course to convert the object: $a = (array)$a;

Upvotes: 2

Script47
Script47

Reputation: 14530

extends ArrayObject or implements ArrayAccess so you can access the object as array.

Reading Material

ArrayObject

ArrayAccess

Upvotes: 2

Related Questions