Skip to content
Snippets Groups Projects
Commit 3294a36e authored by MARCO Jonathan's avatar MARCO Jonathan
Browse files

:sparkles: Create POO

parent 13e0c2e8
Branches
No related merge requests found
<?php
include 'User.php';
class Database
{
private PDO $pdo;
public function __construct()
{
$absolutePath = dirname(__FILE__);
$this->pdo = new PDO("sqlite:$absolutePath/database.sqlite");
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function query(string $request)
{
return $this->pdo->query($request);
}
public function getUsers(): Array
{
$users = $this->pdo->query('SELECT * FROM user')
->fetchAll();
$userObject = [];
foreach ($users as $user) {
$userObject[] = new User($user);
}
return $userObject;
}
}
\ No newline at end of file
<?php
class User {
private int $id;
private string $email;
private string $password;
public function __construct(array $credentials)
{
$this->id = $credentials['id'];
$this->email = $credentials['email'];
$this->password = $credentials['password'];
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}
/**
* @param string $email
*/
public function setEmail(string $email): void
{
$this->email = $email;
}
/**
* @return string
*/
public function getPassword(): string
{
return $this->password;
}
/**
* @param string $password
*/
public function setPassword(string $password): void
{
$this->password = $password;
}
}
\ No newline at end of file
......@@ -31,16 +31,16 @@
<?php
require_once 'assets/php/db.php';
require_once 'assets/php/Database.php';
$users = $pdo->query('SELECT * FROM user')
->fetchAll();
$database = new Database();
$users = $database->getUsers();
foreach ($users as $user) :
?>
<article>
<p><?= $user['email'] ?></p>
<p><?= $user['password'] ?></p>
<p><?= $user->getEmail() ?></p>
<p><?= $user->getPassword() ?></p>
</article>
<?php
endforeach;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment