// The DataLayer to MySQL
require_once $_SERVER["DOCUMENT_ROOT"] . "/api/DAL/classes/User.dal.php";
class User
{
public $id;
public $name;
public $age;
public function __construct($id = null)
{
$this->dl = new UserDAL();
// Get an existing user object
if (!empty($id)) {
$user = $this->getUsers($id);
$this->id = $user->id;
$this->name = $user->name;
$this->age = $user->age;
}
}
public function create($user)
{
// Create new user
$this->name = $user->name;
$this->age = $user->age;
return $this->dl->create($user);
}
public function update($user)
{
// Update existing user
$this->id = $user->id;
$this->name = $user->name;
$this->age = $user->age;
return $this->dl->update($user);
}
public function delete($user)
{
// Create new user
$this->id = $user->id;
return $this->dl->delete($user);
}
public function getUsers($id)
{
return $this->dl->getUsers($id);
}
}
// Some tests...
// Get existing user
$existing_user = new User("1");
var_dump($existing_user);
// Create a new user
$new_user = new User();
$new_user->age = 23;
$new_user->name = "Bob";
$new_user->create($new_user);
var_dump($new_user);
// Update existing user
$update_user = new User();
$update_user->id = "1";
$update_user->name = "Henry";
$update_user->age = 100;
$update_user->update($update_user);
var_dump($update_user);
// Delete a user
$delete_user = new User();
$delete_user->delete("1");
User.dal.php
too, as that would make this a more interesting question. \$\endgroup\$getUsers()
method, is the$id
argument/value an array or integer I know it's not an array)? I ask becausegetUsers()
implies that you're returning multiple users, not just one. Otherwise it'd be namedgetUser()
See the discrepancy and potential confusion? \$\endgroup\$