Stateful User tracking I have a class that takes a string or an integer, which stand for username or user id in the database.  I am not comfortable with my constructor taking "a string or an integer" and want to modify it to be more type-specific.  But how do I do so?

class User
{

    /**
     *
     * @var string|int
     */
    private $id;    

    /**
     *
     * @var array
     */
    private $udata;

    /**
     *
     * @param string|int $var            
     */
    function __construct($var)
    {
        $this-&gt;id = 0;
        $this-&gt;lookup($var);
    }

    /**
     *
     * @param string|int $var            
     */
    function lookup($var)
    {
        $sql = "SELECT * FROM user WHERE " 
            . (is_numeric($var) ? 'id' : 'username') . " = " . db_input($var);
        $row = query($sql);
        $this-&gt;udata = $row;
        $this-&gt;id = $row['id'];
        return $this-&gt;id;
    }
}

/**
 * Uses
 */
$user = new User(51);
$user = new User("JohnDoe");
Create static functions findById and findByUsername and clean-up User constructor or modify it to accept $id and $udata; you can also make it private, if you don't need it outside.

class User {
    // ...

    public static function findById (id) {
        // TODO
    }      

    public static function findByName (name) {
        // TODO
    }
}


$user = User::findById(51)
$user2 = User::findByName("JohnDoe")