John  Dong
John Dong

Reputation: 57

Casting to object

I've been reviewing some code that was made by another developer and I saw some lines that bother me. These are used all throughout the application.

Is there any particular reason why the developer would use type juggling/casting? I'm just curious as I find the code weird.

$object = (object) array();
$result = (object) json_decode($json);

Upvotes: 1

Views: 42

Answers (1)

Shira
Shira

Reputation: 6560

(object) array() is an obscure way to do new stdClass().

(object) json_decode($json) is probably useless since json_decode() already maps objects to stdClass by default. This depends on the data being decoded.

Upvotes: 1

Related Questions