SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: Performance - Array VS Object
-
Oct 12, 2009, 11:25 #1
- Join Date
- Feb 2007
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Performance - Array VS Object
It seems like:
PHP Code:$array['key'] = 'value'
PHP Code:$object->key = 'value'
Thank!
-
Oct 12, 2009, 12:25 #2
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Oct 12, 2009, 12:32 #3
- Join Date
- Feb 2007
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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?
-
Oct 12, 2009, 13:22 #4
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Oct 12, 2009, 13:48 #5
- Join Date
- Feb 2007
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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?
-
Oct 12, 2009, 14:02 #6
- Join Date
- Aug 2005
- Location
- Winnipeg
- Posts
- 498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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 cleanerThe only constant in software is change itself
-
Oct 12, 2009, 14:33 #7
- Join Date
- Mar 2008
- Posts
- 1,149
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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