I have a static getUser($userID) method in my User class which queries the database and returns a User object for the given user ID.
At times, I have multiple users I need to obtain at the same time. I've come up with two options:
In both cases, I will be returning an array of User objects. But what should happen if a userID is in valid or doesn't exist? Just exclude that User from the array? Seems weird.
So it would be great to get answers to:
Feel free to critique the use of a static method getUser() too, I don't know where else to put that method, thanks.
This is something I don't think you should optimize until it becomes a bottle neck.
It is much cleaner if you split it up. In fact, I would even recommend that you create another folder and call it collections for example. In there you have for example a UserCollection that allows you to iterate over different sets of users. With the current() method you then return a user object.
For the getUser() part, I would put things like a user object into a folder called entities. Those objects handle the CRUD operations of a single user while the collections are there to return sets of users (or a single user).
In the entity part you can throw an exception if something is not found. The collection should only return valid IDs.
As a sidenote, you can also mix different objects in your collection when you use iterators, this can be pretty useful in some cases.