Какая реализация грамотней?
1. Вариант номер раз.
class News
{
private $id;
private $properties = array();
public function __construct($id = NULL)
{
if ($id === NULL)
return;
$this->id = $id;
$result = mysql_query('SELECT * FROM `news` WHERE `id`="'.$id.'" LIMIT 1');
if (!$row = mysql_fetch_assoc($result)) {
unset($this->id)
return;
}
$this->properties = mysql_fetch_assoc($result);
}
public function add($data)
{
$this->properties = $data;
mysql_query('INSERT INTO `news` …');
$this->id = mysql_insert_id();
return $this->id;
}
public function edit($data)
{
if (!isset($this->id) || !$this->id)
return false;
$this->properties = $data;
mysql_query('UPDATE `news` … WHERE `id`="'.$this->id.'" LIMIT 1');
return true;
}
public function delete()
{
if (!isset($this->id) || !$this->id)
return false;
mysql_query('DELETE FROM `news` WHERE `id`="'.$this->id.'" LIMIT 1');
return true;
}
}
2. Вариант номер два.
class News
{
private $id;
private $properties = array();
public function __construct($id)
{
$this->id = $id;
$result = mysql_query('SELECT * FROM `news` WHERE `id`="'.$id.'" LIMIT 1');
if (!$row = mysql_fetch_assoc($result)) {
unset($this->id)
return;
}
$this->properties = mysql_fetch_assoc($result);
}
static function add($data)
{
mysql_query('INSERT INTO `news` …');
return mysql_insert_id();
}
public function edit($data)
{
if (!isset($this->id) || !$this->id)
return false;
$this->properties = $data;
mysql_query('UPDATE `news` … WHERE `id`="'.$this->id.'" LIMIT 1');
return true;
}
public function delete()
{
if (!isset($this->id) || !$this->id)
return false;
mysql_query('DELETE FROM `news` WHERE `id`="'.$this->id.'" LIMIT 1');
return true;
}
}
3. Вариант номер три.
class News
{
private $id;
private $properties = array();
public function __construct($id)
{
$this->id = $id;
$result = mysql_query('SELECT * FROM `news` WHERE `id`="'.$id.'" LIMIT 1');
if (!$row = mysql_fetch_assoc($result)) {
unset($this->id)
return;
}
$this->properties = mysql_fetch_assoc($result);
}
public function edit($data)
{
if (!isset($this->id) || !$this->id)
return false;
$this->properties = $data;
return true;
}
public function save_to_db()
{
if (!isset($this->id) || !$this->id)
return false;
mysql_query ('UPDATE `news` … WHERE `id`="'.$this->id.'" LIMIT 1');
return true;
}
static function add($data)
{
mysql_query('INSERT INTO `news` …');
return mysql_insert_id();
}
static function delete($id)
{
mysql_query('DELETE FROM `news` WHERE `id`="'.$id.'" LIMIT 1');
return true;
}
}