【发布时间】:2015-09-30 10:02:39
【问题描述】:
在许多 php 框架中,我们可以这样做:
class Test extends Model
{
protected $first;
protected $second;
public function getAllAttributes() {
return $this->first.','.$this->second;
}
}
如何在我自己的解决方案中做到这一点?我目前的模型:
class Model
{
// ..
public function all() {
return mysqli_query($con, 'select * from '.$this->table);
}
}
用法:
$test = new Test();
$result = $test->all();
foreach ($result as $t) {
echo $t->getAllAttributes();
}
【问题讨论】:
-
但我认为他们可能将值存储在来自构造函数的变量中,或者其他地方并没有神奇地出现在这些变量中
-
我认为您想(或可以)使用 PDO 的
setFetchMode将结果直接选择到预定义的类中 (PDO::FETCH_CLASS) -
哇,看起来很棒!谢谢。