从方法中获取并回显多个字符串

I'm fairly new with PHP and I am trying to understand working Classes, Methods and Properties. I've a problem that I want to get solved and that I don't get. I was unable to find a proper solution (that I also liked).

Before I ask my question here are my classes/code:

Main class User (user.class.php)

 class User {

    // Properties
    protected $_name;
    public $surname;

    // Methods
    public function setName($name) {
        $this->_name = $name;   
    }   

    public function getName() {
        return $this->_name;
    }

    public function setSurname($surname) {
        $this->surname = $surname;
    }

    public function getSurname() {
        return $this->surname;
    }

}

Child class *Premium_User* (premiumUser_child_user.class.php)

class Premium_User extends User { 
    private $_premiumId;

    public function setData($name, $id) {
        $this->name = $name;
        $this->_premiumId = $id;
    }   

    public function getData() {
        return array(
        'name' => $this->name,
        'id' => $this->_premiumId
        );

    }   
}

The code:

 // Class User::

include('user.class.php');

// Child class -> class::User

include('premiumUser_child_user.class.php');

$user = new Premium_User(); 
$user->setData('P','1'); 
$user->setData('H','2'); 
$user->setData('P','3'); 

$getUserData = $user->getData();

while ($i <= $getUserData['id'] ) {
    echo "User: " . $getUserData['name'] . " ID: " . $getUserData['id'] . ".
"; <br>
    $i++; 
}

The output:

User: P ID: 3. 
User: P ID: 3. 
User: P ID: 3.

What I would like to get it is:

User: P ID: 1.
User: H ID: 2. 
User: P ID: 3.

I tried to use *array_push* to add every entry to the array, but didn't work.

You only create one single user instead of three independent ones. If you want to have three users, create three by using new three times.

You call setData() thrice on the same object.

public function setData($name, $id) {
    $this->name = $name;
    $this->_premiumId = $id;
}

By this definition, you overwrite the changes from the previous calls to the setData method. I have commented the lines that have no effect for you:

$user = new Premium_User();
// $user->setData('P','1');
// $user->setData('H','2');
$user->setData('P','3');

To have multiple users accessible through a single variable, you can put your users into an array:

$user1 = new Premium_User();
$user1->setData('P','1');
$user2 = new Premium_User();
$user2->setData('H','2');
$user3 = new Premium_User();
$user3->setData('P','3');

For use with PHP 5.4 and later:

$users = [$user1, $user2, $user3];
foreach($users as $user) {
    echo "User: " . $user->getData()['name'] . " ID: " . $user->getData()['id'] . ".
";
}

Before PHP 5.4:

$users = array($user1, $user2, $user3);
foreach($users as $user) {
    $userData = $user->getData(); 
    echo "User: " . $userData['name'] . " ID: " . $userData['id'] . ".
";
}

the class only remembers the last time you set the data. either instantiate multiple users or set them one at a time and output them one at a time.

$user = new Premium_User();
$user->setData('P','1'); 

$user2 = new Premium_User();
$user2->setData('H','2'); 

$user3 = new Premium_User();
$user3->setData('P','3'); 

FYI the <br> tags are not needed until you use echo.

Or use:

$user = new Premium_User();
$user->setData('P','1');
//do something with this user data
$user->setData('H','2'); 
//do something with second user data
//etc.