【发布时间】:2018-07-06 14:07:16
【问题描述】:
我遇到了多种完成 setter 的方法,但我还没有在大型项目中使用它们...
我想知道这两个setter有什么区别,哪一个最好?
public function setId($id){
if(is_int($id) && $id > 0){
$this->_id = $id;
return true;
}
return false;
}
或者
public function setId($id){
$id = (int) $id;
if($id > 0){
$this->_id = $id;
return true;
}
trigger_error("The id of your object could not be set/assigned !);
}
trigger_error() 函数的用途是什么?当到处都有代码时,这些 setter 中的哪一个是有益的
注意:我正在使用 hydrate() 函数用数据库中的数据填充我的对象!
【问题讨论】:
-
非常有用,非常感谢!
标签: php oop casting getter-setter setter