title: How to implement factory design pattern
body: <p>I am trying to apply the factory design pattern to a real world problem in an application. I am using data objects (kind of like ORM but not quite) where an object represents a row from the database and its properties are the columns. Having said this - I have 3 different types of users which all extend the base class <code>User</code> and are all kept in the same database table of <code>users</code>. I have an extra column <code>type</code> that I use to determine the type of user.</p>
  
  <p>So to fetch a user with id 1 I would use this code:</p>
  
  <pre><code>$user = User::getByPK(1);
  </code></pre>
  
  <p>However the different types of users have different properties and methods and I need to get the appropriate instance, which is where I am stuck. I created a factory class but I am not quite sure how to call it. I need to have the information about the user from the database before I can determine what type of user it is, however I need to instantiate the appropriate class and these two things are kind of dependant of each other. </p>
  
  <p>Here is what I made - I get the appropriate instance but all of the information for the user is kept in the base class.</p>
  
  <pre><code>class UserFactory {
  
      public function getUser($type) {
  
          switch($type) {
              case User::ORDINARY:
                  return new OrdinaryUser();
  
              case User::MODERATOR:
                  return new Moderator();
  
              case User::ADMINISTRATOR:
                  return new Administrator();
          }
      }
  }
  
  
  $user = User::getByPK(1); // Has all the information
  
  $factory = new UserFactory();
  
  $object = $factory-&gt;getUser($user-&gt;type); // Is instance of Administrator
  </code></pre>
  
  <p>What is the proper way to use the factory pattern in this case?</p>