Też nie ma sensu i też nie jest to OOP. Powinna być osobna klasa odpowiedzialna za bazę danych (czyli w niej metody np. "select", "query", ta klasa łączy się z bazą danych). Jest kilka możliwości rozwiązania problemu, np ActiveRecord, DAO. Przykładowo (pseudokod):
<?php
class Database{
//uchwyt bazy danych
protected $hConn;
public function __construct(){
//polaczenie z bd
}
public function query($sql){
return $query($sql, $this->hConn);
}
//jakieś inne metody
}
interface DAO{
public function getById($id);
public function delete();
public function getAll();
public function save(RowObject $o);
}
abstract class SqlDao implements DAO{
protected $className;
protected $tableName;
protected $db;
public function __construct($className, $tableName, Database $db){
$this->className = (string) $className;
$this->tableName = (string) $tableName;
$this->db = $db;
}
public function getById($id){
$table = $this->tableName;
$class = $this->className;
$sql = "SELECT * FROM $table WHERE id='$id';";
$res = $this->db->query($sql);
return new $class($record);
}
public function delete(RowObject $o){
//implementacja
}
public function getAll(){
//implementacja
}
public function save(RowObject $o){
//implementacja
}
}
interface RowObject{
public function getId();
}
abstract class AbstractRowObject implements RowObject{
protected
$fields = array(); protected
$data = array();
$this->fields = $fields;
$this->data = $data;
}
public function __get($name){
if(array_value_exists($name, $this->fields)){
return $this->data[$name];
}
}
public function __set($name, $value){
if(array_value_exists($name, $this->fields)){
$this->data[$name] = $value;
}
}
public function getId(){
return $this->data['id'];
}
}
class CinemaDAO extends SqlDao{
public function __construct(Database $sql){
parent::__construct('cinema', 'Cinema', $sql);
}
}
class Cinema extends AbstractRowObject{
public function __construct
($data = array()){ parent
::__construct
(array('id', 'name', 'location'), $data); }
}
?>
zastosowanie:
<?php
$dao = new CinemaDao($db);
$cinema = $dao->getById($id);
//lub
$c = new Cinema();
$c->name = "nazwa";
$c->location = "lokacja";
$dao->save($c);
?>
Na forum jest dosyć dużo rozwiązań tego "problemu", więc szukaj...