title: php OOP best practice with subclasses of a generic user class
body: <p>I while back I worked on a project what build a system around two different types of Users. Let's call the types Men and Woman for the simplicity of it. </p>
  
  <p>We had three different classes. A User class that held the common information about all users (such as name, age and so on). This class also held a property of the type of the user man or woman). The other two classes Man and Woman extended User. </p>
  
  <p>The database contained three tables. One for the User objects, one for Men objects and one for Woman objects. </p>
  
  <p>The problem that I'm thinking about now is how to best create instances of a Man or Woman (based on a User-id unique for all Users). Since we wanted to create an instance of either Man or Woman depending on the user type we did it with a function that first checked the type of the user and then returned an object or either Man or Woman.</p>
  
  <p>Perhaps to better explain it, it looked a little like this:</p>
  
  <pre><code>class User {
       private $id, $name;
       function __construct($userId) {
            // Get the information from the database and put it in instance variables
       }
  }
  
  class Man extends User {
      private $somethingManSpecific;
      function __construct($userId) {
           parent::__construct($userId);
           // Fetch info from the Man DB-table...
      }
  }
  
  class Woman extends User {
      private $somethingWomanSpecific;
      function __construct($userId) {
           parent::__construct($userId);
           // Fetch info from the Woman DB-table...
      }
  }
  
  function getUser($userId) {
      $type = find_the_type_of_user($userId); 
      if ($type == "man") return new Man($userId);
      else return new Woman($userId);
  }
  
  $fooUser = getUser(99); 
  </code></pre>
  
  <p>This feels lika a weird way of doing it though and I wonder if there is a better solution for this. </p>
  
  <p>Part of my question is of course how a class constructor should be seen. We used the constructor to retrieve all the attributes of the object from the database - so if you have any feedback on that it would be much appreciated.</p>
  
  <p>Thank you!  </p>