【发布时间】:2009-01-21 14:19:01
【问题描述】:
如果在实例化过程中给出了无效参数,我在让我的对象正常失败时遇到一些问题。我有一种感觉,这是我需要重新审视的地方的一个小语法。任何帮助都将不胜感激。
class bib {
protected $bibid;
public function __construct($new_bibid) {
if(!$this->bibid = $this->validate_bibid($new_bibid)) {
echo 'me';
return false;
}
//carry on with other stuff if valid bibid
}
private static function validate_bibid($test_bibid) {
//call this method every time you get a bibid from the user
if(!is_int($test_bibid)) {
return false;
}
return (int)$test_bibid;
}
}
请注意,我在那里有一个“echo me”行来证明它实际上是返回 false。我在我的 PHP 中调用它的方式如下:
if(!$bib=new bib('612e436')) {
echo 'Error Message';
} else {
//do what I intend to do with the object
}
这会从上面输出 me,然后继续进入 else 块,执行我打算对有效对象执行的操作。
谁能发现我在里面做错了什么?
谢谢!
【问题讨论】:
-
在构造函数的第一行发现另一个问题