正确的实现方式:DAO Design Pattern php + pdo

class UserMapper {

    protected $db;

    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function save(User $user_object)
    {

    }


    public function getUserById($id)
    {
        //code
    }


}

class User {

    private $id;
    private $username;

    function __construct($user_row = null) 
    {
        if (!is_null($user_row)) {
            $this->id = $user_row->id;
            $this->username = $user_row->username;
        }
    }

    public function __set($name, $value)
    {
    }

    public function __get($name)
    {
        return $this->$name;
    }

}

Example:

$user = new User();
$user->username = 'New Username';
$user_mapper = new UserMapper($db); //Pdo object = $db
$user_mapper->save($user);

Is this the correct way of implenting the pattern?

Well, that's about it but there's more to the pattern .You might want to check out this article and these references, the pattern is explained in depth there.