Witam!
Proszę szanowne grono o ocenę mojego czatu napisanego w OOP, dopiero się uczę, z chęcią przyjmę wszystkie uwagi (szczególnie krytyczne) dot. kodu a także co mogłoby być lepiej
config.php
<?php
//DANE DO POŁĄCZENIA Z BAZĄ
class Config{
public static $db_host = 'localhost'; public static $db_name = 'Czat'; public static $db_user = 'root'; public static $db_pass = 'root';
}
?>
database.php
<?php
require_once 'config.php';
abstract class database
{
protected $pdo;
public $error;
public function __construct()
{
// POLACZENIE Z BAZA
try
{
$this->pdo = new PDO('mysql:host='.Config::$db_host.';dbname='.Config::$db_name, Config::$db_user, Config::$db_pass);
$this->pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>"; }
}
protected function insert($author, $msg)
{
$stmt = $this->pdo->prepare('INSERT INTO Czat (Author, Msg) VALUES (:auth, :msg)');
$stmt->bindParam(':auth', $author);
$stmt->bindParam(':msg', $msg);
if($stmt->execute())
{
return TRUE;
}
else
{
return FALSE;
}
}
protected function select()
{
$messages = $this->pdo->query('SELECT * FROM Czat');
return $messages;
}
public function __destruct()
{
$this->pdo = NULL;
}
}
?>
czat.php
<?php
require_once 'database.php';
class czat extends database
{
public $error ='';
public function __construct() {
parent::__construct();
}
public function add_message($author, $msg)
{
{
$escaped = $this->escape($author, $msg); // escapujemy
if($this->insert($escaped[0], $escaped[1])){
$this->error = "DODANO";
}
else{
$this->error = "NIE DODANO";
}
}
}
public function get_messages()
{
return $this->select();
}
public function escape($val1, $val2)
{
return array($auth, $msg); // ZWRACAMY WYESCAPOWANA TABLICE }
}
?>
pozdrawiam!