php - 根据准备好的语句的结果分配对象属性
问题描述
我想创建我的类的一个新实例,并将返回的值分配给它的属性。这样做的原因是我正在创建一系列从调用类继承的方法,而不是使用我已经工作过的静态方法。
我目前使用的示例:
public static function findById($id) {
$id = self::escapeParam($id);
$idVal = is_int($id) ? "i" : "s";
$sql = "SELECT * FROM ".static::$db_table." WHERE id = ? LIMIT 1";
return static::findByQuery($sql,$idVal,$id);
}
public static function findByQuery($sql,$bindChar = '',$bindVal = '') {
try {
$callingClass = get_called_class();
$object = new $callingClass;
$statement = Database::$connection->prepare($sql);
if(!empty($bindChar)) :
$statement->bind_param($bindChar, $bindVal);
endif;
if($statement->execute()) :
$result = $statement->get_result();
$object = $result->fetch_object();
endif;
$statement->close();
if(!empty($object)) :
return $object;
endif;
} catch(Exception $e) {
}
}
我尝试的是编写一个实例化方法,该方法创建我的类的一个新实例,然后为对象的每个属性分配它从我所做的教程中的数组返回的值。但是,该教程已经过时了,并且没有使用任何新的语法或绑定,所以我试图重新编写它。
以下教程中的示例:
public static function find_by_id($id) {
global $database;
$the_result_array = static::find_by_query("SELECT * FROM " . static::$db_table . " WHERE id = $id LIMIT 1");
return !empty($the_result_array) ? array_shift($the_result_array) : false;
}
public static function find_by_query($sql) {
global $database;
$result_set = $database->query($sql);
$the_object_array = array();
while($row = mysqli_fetch_array($result_set)) {
$the_object_array[] = static::instantation($row);
}
return $the_object_array;
}
public static function instantation($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) {
return property_exists($this, $the_attribute);
}
我在本教程中尝试做的是使用一段时间将我的结果作为数组返回,然后通过将构建的数组传递给static::instantation()
方法来分配一个变量,但它似乎无法正常工作,因为任何我在调用类(例如管理员)中创建的公共函数不会被调用,因为由于类没有被实例化,它们不存在。
解决方案
我现在已经完成了这项工作,尽管我觉得这可能不是最好的方法。
我主要是前端,所以如果有改进或最佳实践,请发表评论。
public static function findByQuery($sql,$bindChar = '',$bindVal = '') {
try {
$statement = Database::$connection->prepare($sql);
if(!empty($bindChar)) :
$statement->bind_param("$bindChar", $bindVal);
endif;
if($statement->execute()) :
$result = $statement->get_result();
$output = $result->fetch_object();
endif;
$statement->close();
if(!empty($output)) :
$class = get_called_class();
$object = new $class;
foreach(get_object_vars($output) as $key => $value) :
$object->$key = $value;
endforeach;
endif;
if(!empty($object)) :
return $object;
endif;
} catch(Exception $e) {
}
}
我最初的想法是声明一个对象,然后我认为 PHP fetch_object 调用会在启动类后将它的属性分配给我的对象,但事实并非如此。
所以我所做的是,如果语句成功并创建了结果对象,然后我使用 get_object_vars() 命令获取对象属性和值,然后将它们作为键值对循环,分配它的每个属性返回值。
我可以确认这是可行的,因为我现在可以从我的删除脚本中运行 $admin->remove(),而不是之前我必须做的 Admin::remove($id);
推荐阅读
- microsoft-graph-api - 尝试读取组文件时收到“拒绝访问”
- c# - 如何避免过多的 != null 检查
- typescript - 当 observable 满足条件时,RxJS 停止 foreach
- angular - 如何使用 Angular Material [mat-form-field] 向文本框插入值
- c# - 使用实体框架从数据库读取数据会导致对象的字段丢失
- php - $_SERVER 变量来识别请求来自哪里?
- javascript - 如何使用 .orderby() 按时间戳对 Firebase 数组中的项目进行排序
- prometheus - 正确检测 Prometheus 计数指标的变化
- sql - 将具有空值的新列添加到大表
- pdf - 编辑默认 knitr pdf