I am new to object oriented coding. I have written my code so that each user is an object. When the user object is created, it sets all of the user objects attributes, some of which take multiple steps and database queries to determine.
I need (I want) to make an array of user objects, so that I can list them, compare them, etc. I am assuming that having many objects in an array will take up more cpu resources that to have many sub-arrays. Since I'll not be modifying the data, or calling any methods, I'm assuming I should convert the objects to arrays. But I can't find instructions (a function) to do this.
My questions are...
1. Does an object take up more cpu resources than an array with the same attributes/elements?
2. If so, how do you convert an object to an array?
If I am over-complicating this, please tell me. Thanks in advance.
How can I convert an object to an array, and should I?
Moderator: General Moderators
You might want to check out this tutorial for some understanding.
Maugrims PHPDataObjects Tutorial
I've probably re-read this tutorial about 5 or 6 times now, and it never gets old. Great stuff on learning about classes and data objects. And it falls in the lines of what you ultimately would like to do.
Maugrims PHPDataObjects Tutorial
I've probably re-read this tutorial about 5 or 6 times now, and it never gets old. Great stuff on learning about classes and data objects. And it falls in the lines of what you ultimately would like to do.
I don't think so... as to converting object to array, take a look at get_object_vars()Does an object take up more cpu resources than an array with the same attributes/elements?
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
It does not take up much more CPU resources - it does consume more memory (another performance measure) but it's not prohibatively expensive. get_object_vars() is very useful for extracting an object's variables which is probably the fastest route to what you want - implement inside a getArray() method for your class and work from there.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US