Hej, jeśli tworzę jakikolwiek projekt obiektowo to wszelkie zapytania do bazy (dodawanie user, komentarzy, ocena) powinnam wywoływać z klasy database.php?
Jeśli mam:
<?php
class Database
{
private static $cont = null;
public function __construct() {
die('Init function is not allowed');
}
public static function connect()
{
// One connection through whole application
if ( null == self::$cont )
{
try
{
self::$cont = new PDO('mysql:host=localhost;dbname=baza;encoding=utf8', 'root','');
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont = null;
}
}
?>
I chce dodać np funkcje addUser, to powinno to wygladać tak?:
<?php
class Database
{
private $pdo;
private static $cont = null;
public function __construct() {
die('Init function is not allowed');
}
public static function connect()
{
// One connection through whole application
if ( null == self::$cont )
{
try
{
self::$cont = new PDO('mysql:host=localhost;dbname=baza ;encoding=utf8', 'root','');
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public function addUser() {
this->connect();
$stmt = $this->pdo->prepare("INSERT INTO $this->table (Name, Category, Price) VALUES (:name, :category, :price)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':category', $category);
$stmt->bindParam(':price', $price);
$stmt->execute();
}
public static function disconnect()
{
self::$cont = null;
}
}
?>
Wytłumaczy mi ktoś dobrze jak to robić, żeby już od początku nauki z tym dobrze robić?