It seems like:
...executes faster than:PHP Code:$array['key'] = 'value'
I'm wondering why is that and if you have any performance optimization tips for objects.PHP Code:$object->key = 'value'
Thank!
| SitePoint Sponsor |
It seems like:
...executes faster than:PHP Code:$array['key'] = 'value'
I'm wondering why is that and if you have any performance optimization tips for objects.PHP Code:$object->key = 'value'
Thank!
An object is a richer data type than an array. Because of that, I would expect more overhead since it provides more features. The difference really should be negligible in your example though. Is it not? If you need the functionality of an object, use it.
Thanks! That makes sense.
Here's another question. What are the basic advantages of objects in terms of functionality? At what point do you say - this should no longer be an array, it should rather be an object?
Well, you usually don't use objects unless you intend to write some code that uses object oriented principals. I realize I just beat around the bush with that answer, but I don't think I could really get the point across in a summary. I don't feel OOP is something people generally understand well until you put in a decent amount of effort studying.
If you're using an object because you like the syntax of $foo->key better than $foo['key'], my advice is don't.
I see your point, but it brings another question. If arrays are better for storing data, why does the mysql_fetch_object method exist and why would anyone use that method over mysql_fetch_assoc?



An object will have a slight overhead compared to an associative array, mostly because you need to also dereference the 'this' pointer upon access.
As for memory allocation, there shouldn't be much of a difference, although I have heard others claim there was.
Like it's been said, the performance gain would be negligable and not worth worrying about. Use objects their cleaner![]()
The only constant in software is change itself





If you use objects rather than array, you can better enforce a structure, and generating docs about it is also easier. I try to use objects in that case, although nothing beats the speed of arrays, so I still use arrays a lot.
Bookmarks