Here is my index.php
<?
require("Annonce.php");
$annonce = new Annonce();
$annonce->id=1;
$annonce->delete();
?>
My Annonce.php is!
<?php
require("DB.php");
class Annonce extends DB {
}
?>
and finally DB is:
<?php
$db = new PDO('mysql:host=localhost;dbname=annonces;charset=utf8', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
class DB {
public static $debug = true;
function __construct() {
# code...
}
function delete() {
$id = $this->id;
$table = get_class($this);
$sql="delete from $table where id=$id";
if ($this::$debug) echo $sql;
}
function get() {
$sql="select *...";
}
}
?>
I don't know what is the best strategy to define the $db connection ?
If I define it inside DB class, it will make a connection any time an "Annonce" object is created. Maybe using $db as GLOBAL (which I think is not clean)
Any suggestion for that ?