0
我的想法是創建一個父類Db_object並將一些方法用於子類。我第一次提出的子類用戶和測試所有的方法,他們都工作得很好,但是當我嘗試使用其他類(照片)類相同的方法,我不能用create()
,update()
& delete()
。無法在第二個子類中使用父類方法
我是編程新手,沒有太多經驗。以下是我迄今所做:
Db_object(家長):
<?php
class Db_object
{
public static function find_all()
{
return static::find_by_query("SELECT * FROM " . static::$db_table . " ");
}
public static function find_by_id($user_id)
{
global $database;
$test_array = array();
$the_result_array = static::find_by_query("SELECT * FROM " . static::$db_table . " WHERE id=$user_id LIMIT 1");
return !empty($the_result_array) ? array_shift($the_result_array) : $test_array;
}
public static function find_by_query($sql)
{
global $database;
$result_set = $database->query($sql);
//We created an empty array, so that we can store values in it
$the_object_array = array();
/*if($result_set === FALSE)
{
die("Error: " . mysqli_error());
}
if (!empty($result_set))
{*/
// We use while loop to fetch the database table
while($row = mysqli_fetch_array($result_set))
{
$the_object_array[] = static::instantiation($row);
}
/*}*/
return $the_object_array;
}
//instantiation method loops through the databse record & assign those to object properties.
public static function instantiation($the_record)
{
$calling_class = get_called_class();
$the_object = new $calling_class;
foreach ($the_record as $the_attribute => $value)
{
if($the_object->has_the_attribute($the_attribute))
{
$the_object->$the_attribute = $value;
}
}
return $the_object;
}
private function has_the_attribute($the_attribute)
{
$object_properties = get_object_vars($this);
return array_key_exists($the_attribute, $object_properties);
}
//Method to get all the properties.
protected function properties()
{
$properties = array();
foreach (static::$db_table_fields as $db_field)
{
if (property_exists($this, $db_field))
{
$properties[$db_field] = $this->$db_field;
}
}
return $properties;
}
//We are looping through protected static $db_table_fields.
protected function clean_properties()
{
global $database;
$clean_properties = array();
foreach($this->properties() as $key => $value)
{
$clean_properties[$key] = $database->escape_string($value);
}
return $clean_properties;
}
public function save()
{
return isset($this->id) ? $this->update() : $this->create();
}
public function create()
{
global $database;
$properties = $this->clean_properties();
$sql = "INSERT INTO " . static::$db_table . "(" . implode(",", array_keys($properties)) . ")";
$sql .= "VALUES ('" . implode("','", array_values($properties)) . "')";
if ($database->query($sql))
{
//This method is responsible for pulling up the last query, then assigmimg the id to the object.
$this->id = $database->the_insert_id();
return true;
}
else
{
return false;
}
}
public function update()
{
global $database;
$properties = $this->clean_properties();
$property_pairs = array();
foreach ($properties as $key => $value)
{
$property_pairs[] = "{$key}='{$value}'";
}
$sql = "UPDATE " . static::$db_table . " SET ";
$sql .= implode(", ", $property_pairs);
$sql .= " WHERE id= " . $database->escape_string($this->id);
$database->query($sql);
return (mysqli_affected_rows($database->connection) == 1) ? true : false;
}
public function delete()
{
global $database;
$sql = "DELETE FROM " . static::$db_table . " ";
$sql .= "WHERE id=" . $database->escape_string($this->id);
$sql .= " LIMIT 1";
$database->query($sql);
return (mysqli_affected_rows($database->connection) == 1) ? true : false;
}
}
?>
用戶(一切似乎做工精細):
<?php
include ("init.php");
class User extends Db_object
{
protected static $db_table = "users";
protected static $db_table_fields = array('username', 'password', 'first_name', 'last_name');
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
public static function verify_user($username, $password)
{
global $database;
//To senatize Username & Password.
$username = $database->escape_string($username);
$password = $database->escape_string($password);
$sql = "SELECT * FROM " . self::$db_table . " WHERE ";
$sql .= "username = '{$username}' ";
$sql .= "AND password = '{$password}' ";
$sql .= "LIMIT 1";
$the_result_array = self::find_by_query($sql);
return !empty($the_result_array) ? array_shift($the_result_array) : false;
}
}
?>
照片(創建,更新和刪除不起作用):
<?php
//include ("init.php");
class Photo extends Db_object
{
protected static $db_table = "photos";
protected static $db_table_fields = array('photo_id', 'title', 'description', 'file_name', 'type', 'size');
public $photo_id;
public $title;
public $description;
public $file_name;
public $type;
public $size;
}
?>
而且,這是query()
方法,從數據庫類:
public function query($sql)
{
$result = mysqli_query($this->connection, $sql);
return $result;
}
我沒有得到任何類型的錯誤。如果代碼中有任何混淆,請問,我會盡力解釋。
你可以分享你的錯誤代碼的一部分嗎? – iwaduarte
根據我的說法,錯誤應該在這個方法中:'public static function find_by_query($ sql) { global $ database; $ result_set = $ database-> query($ sql); $ the_object_array = array(); 而($行= mysqli_fetch_array($ result_set)) { $ the_object_array [] =靜態::實例化($行); } return $ the_object_array; }' – user8077853
我會從不使用'$ database'作爲全局。我會注入或靜態連接。' – Rasclatt