提问人:tj12 提问时间:12/13/2017 更新时间:12/13/2017 访问量:98
如何在对象中设置静态变量变量?
How do you set a static variable variable in object?
问:
我有一个对象 Obj,其方法为 sql(),用于创建 SQL 查询以更新数据库。数据库中的表具有其 ID 字段的唯一名称。我的目标是能够在子对象中定义表名和 ID 字段名。
class Obj {
public static function sql(){
$attributes = ['something', 'else', 'whatever'];
foreach($attributes as $key => $value){
$attribute_pairs[] = "{$key}='{$value}'";
}
$sql = "UPDATE " . static::$table_name . " SET ";
$sql .= join(", ", $attribute_pairs);
$sql .= " WHERE " . static::$id_name . "='" . static::$id_name . "' ";
return $sql;
}
}
class ChildObj extends Obj{
protected static $table_name="accounts";
protected static $id_name = "account_id";
}
$test = new ChildObj;
echo $test::sql();
//returns UPDATE accounts SET 0='something', 1='else', 2='whatever' WHERE account_id='account_id'
上面代码的问题是我无法弄清楚如何引用 ID 字段的值。特别是这一行:
$sql .= " WHERE " . static::$id_name . "='" . static::$id_name . "' ";
返回以下内容:
account_id='account_id'
我需要它返回“account_id”的值
account_id='#'
答: 暂无答案
评论