Programmation Orientée Objets
Bonjour,
Je suis un tutoriel sur la programmation orientée objet en php. Mais voila, je rencontre un petit problème avec mon code.
Voici mon fichier users.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
<?php
$users = App::getInstance()->getTable('users')->getAll();
?>
<div id="contenu">
<table>
<thead>
<tr>
<th>
<a>Nom</a>
</th>
<th>
<a>Nom d'utilisateur</a>
</th>
<th>
<a>Groupe</a>
</th>
<th>
<a>Désactivé</a>
</th>
</tr>
</thead>
<tbody>
<?php
foreach ($users as $user):
?>
<tr id="user_<?= $user->id_user ?>" onclick="window.location.href='<?= $user->url ?>'">
<td> <?= $user->name_user ?> </td>
<td> <?= $user->username_user ?> </td>
<td> <?= $user->name_group ?> </td>
<td class="boolean">
<input type="checkbox" <?= ($user->active_user) ? 'checked' : '' ?> disabled="">
</td>
</tr>
<? endforeach; ?>
</tbody>
<tfoot></tfoot>
</table>
</div> |
Ce qui ne fonctionne pas c'est lorsque je fais cette ligne, je me retrouve avec une page blanche.
Voici mes autres fichiers:
App.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
<?php
use Core\Config;
use Core\Database;
class App{
private static $_instance;
private $db_instance;
public static function getInstance(){
if(is_null(self::$_instance)){
self::$_instance = new App();
}
return self::$_instance;
}
public static function load(){
session_start();
require ROOT . '/app/Autoloader.php';
\App\Autoloader::register();
require ROOT . '/core/Autoloader.php';
\Core\Autoloader::register();
}
public function getTable($name){
$class_name = '\\App\\Model\\Model' . ucfirst($name);
return new $class_name($this->getDb());
}
public function getDb(){
$config = Config::getInstance(ROOT . '/config/config.php');
if(is_null($this->db_instance)){
$this->db_instance = new Database($config->get('db_name'), $config->get('db_user'), $config->get('db_pass'), $config->get('db_host'));
}
return $this->db_instance;
}
} |
ModelUsers.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
<?php
namespace App\Model;
use Core\Model\Model;
class ModelUsers extends Model{
public function __get($name)
{
$method = 'get' . ucfirst($name);
$this->$name = $this->$method();
return $this->$name;
}
public function getUrl(){
return 'index.php?p=users.edit&id='. $this->id_user;
}
} |
Model.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
<?php
namespace Core\Model;
use Core\Database;
class Model{
protected $table;
protected $db;
public function __get($name)
{
$method = 'get' . ucfirst($name);
$this->$name = $this->$method();
return $this->$name;
}
public function __construct(Database $db)
{
$this->db = $db;
if(is_null($this->table)){
$class_parts = explode('\\', get_class($this));
$class_name = end($class_parts);
$this->table = strtoupper(str_replace('Model', '', $class_name));
}
}
public function query($statement, $attributes = null, $one = false){
if($attributes){
return $this->db->prepare($statement, $attributes, get_called_class(), $one);
}else{
return $this->db->query($statement, get_called_class(), $one);
}
}
public function getAll(){
return $this->query("SELECT * FROM " . $this->table);
}
} |
Database.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
<?php
namespace Core;
use \PDO;
class Database
{
private $db_name;
private $db_user;
private $db_pass;
private $db_host;
private $pdo;
public function __construct($db_name, $db_user, $db_pass, $db_host)
{
$this->db_name = $db_name;
$this->db_user = $db_user;
$this->db_pass = $db_pass;
$this->db_host = $db_host;
}
private function getPDO()
{
if ($this->pdo === null) {
$pdo = new PDO('mysql:dbname=' . $this->db_name . ';host=' . $this->db_host, $this->db_user, $this->db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo = $pdo;
}
return $this->pdo;
}
public function query($statement, $class_name = null, $one = false)
{
$req = $this->getPDO()->query($statement);
if($class_name === null){
$req->setFetchMode(PDO::FETCH_OBJ);
}else{
$req->setFetchMode(PDO::FETCH_CLASS, $class_name);
}
if ($one) {
$data = $req->fetch();
} else {
$data = $req->fetchAll();
}
return $data;
}
public function prepare($statement, $attributes, $class_name = null, $one = false)
{
$req = $this->getPDO()->prepare($statement);
if($class_name === null){
$req->setFetchMode(PDO::FETCH_OBJ);
}else{
$req->setFetchMode(PDO::FETCH_CLASS, $class_name);
}
$req->execute($attributes);
if ($one){
return $req->fetch();
}
else{
return $req->fetchAll();
}
}
} |
Je ne sais pas si vous avez tout ce que vous avez besoin pour m'aider, mais faites le moi savoir.