Why cast `$data` to an object?

#31 · closed · 3 comments

View on GitHub ↗

jtsternberg

Not really an issue, just more of a curiosity, but why cast the var to an object? ```php $wp_query->query_vars[ $var_name ] = (object) $data; ``` https://github.com/GaryJones/Gamajo-Template-Loader/blob/develop/class-gamajo-template-loader.php#L138

Comments

GaryJones

That cast was added in [this commit](https://github.com/GaryJones/Gamajo-Template-Loader/commit/5ed843fdecbec86355816a30099f9f1994e0f908). Looks like I proposed it [this comment](https://github.com/GaryJones/Gamajo-Template-Loader/issues/13#issuecomment-71398976) and it got a couple of 👍 in the thread. As for why: I just thought the code would be cleaner to read to have `$template->foo` than `$template['foo']` - I don't recall there being much reason beyond that, and a preference to use objects over arrays. There does seem to be some type fun going on there - only accepting an array, but casting it as an object. Let me know when you've figured out what old me was thinking :-)

jtsternberg

Ha ha, the only-accepting-arrays bit got me, but yah, I like working w/ objects better as well. Honestly was curious. Any reason not to remove the `array $data` casting at this point?

GaryJones

> Any reason not to remove the array $data casting at this point? None that I can think of.