Hey everyone,
I run into one thing with OOP programming, which I don’t quite understand since it is defined differently by one person than by another.
Suppose we have the following class.
<?php
class User extends DB {
public $username;
public function setUsername($name) {
$this->username = $name;
}
public function getUsername() {
return $this->username;
}
}
$user = new User();
$user->setUsername('jan');
echo $user->getUsername();
?>
This will represent a single user and you can set a user by.
$user = new User();
$user->setUsername('jan');
But let’s say we want to get all users that we have set. We can never get this out of this class since it is about a single object.
May contain a class of multiple names? For example cars, bicycles, users, etc
.
For example, I have the next class: User
<?php
class User extends DB {
public $username;
public $userId;
public function makeNewUser($userName, $userId){
return $this->addtoDB('users', 'username', $userName, 'userId', $userId);
}
public function getUser($userId){
return $this->retrieveFromDb('users', 'userId', $userId);
} }
?>
It extends the DB class. That handles the SQL requests. Now I want to get all Users, no matter what userId
they have.
As far as I know, a single object is the "user"
Which is created in this class? (a single user, one
).
I cannot add the public function getUsers
on the User
class since it is about one
user (not five, ten of thousand).
What I am doing wrong?