Say i have the following data:
// Users Table
+----+-------+----------+
| ID | first | last |
+----+-------+----------+
| 1 | Al | Anderson |
| 2 | Bob | Blakely |
| 3 | Cal | Cambel |
+----+-------+----------+
and the following class:
// Users.php
class Users {
public $first;
public $last;
...
}
I need to instantiate User objects from query results and return an array of objects. I don't know which is the better way to do this:
Static Method
Encapsulate in the class file the following pseudo-code:
public static function getAll() {
$myUsers = array();
...
foreach ($results as $row) {
$user = new User();
...
$myUsers[] = $user;
}
return $myUsers;
}
and call it like this:
$allUsers = Users::getAll();
Regular ol' Function
Works like the static method, but maybe i could refactor to work with other objects as well...
function getAll($objType='Users') {
$myArray = new array();
...
return $myArray;
}
WHICH IS THE BETTER WAY?
I like encapsulating it in a class function, but handling static database connection objects is more pain. I like simple "helper" function, but that just throws OOP to the wind alltogether. What do i do?
***Update: This is not a MySQLi or PDO thing. I'm asking (just from an OOP best practice vantage) is it better to put a static 'getAllOfMyself()' method in classes or better to have simple functions like 'getAllSomethings()'.