2

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()'.

3
  • 1
    Have you considered functions like php.net/manual/en/pdostatement.fetchobject.php? Commented Jul 5, 2012 at 15:36
  • Have you tried looking at the existing PHP libraries? I think Mysqli and PDO can do both things that you would want to do. Also, it's not necessary to always force everything to be classes-objects if you're trying to do something very simple. But then again, there are plenty of built-in OOP based and regular old function based solutions that already exist. It's good to try and find the easiest way! Commented Jul 5, 2012 at 15:36
  • There is no difference between regular functions and static functions. Commented Jul 5, 2012 at 16:15

1 Answer 1

1

My opinion is that you should combine the Factory pattern with Singleton (which is common of factories anyway). Your suggestion of a static method is essentially a factory, but you can/should factor it further in that direction. Make a class called something like UserFactory with methods such as getUserByID() and getAllActiveUsers()... etc. These can (and should be) static methods; however, you can make it a singleton, and each of those methods can call a similarly named private method on the instance (_getUserByID(), _getAllActiveUsers()). The instance can manage your DB connections, and then in the future you can extend it to handle caches, etc. as well.

Then, if you like, you can factor your models into a clean "interface" (i.e. the rest of your application only knows about the "public" interface of a User), and your factory can decide which actual class to instantiate, as long as it conforms to the User interface. This is highly flexible as your model details change over time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.