如果在實例化過程中給出無效參數,我會遇到一些問題讓我的對象正常失敗。我有一種感覺,這是一個小語法的東西,我只是需要新鮮的眼睛。任何幫助都不勝感激。類實例化失敗
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;
}
}
請注意,我有一個'回聲我'行在那裏證明它實際上是返回false。說我在我的PHP調用此方式如下:
if(!$bib=new bib('612e436')) {
echo 'Error Message';
} else {
//do what I intend to do with the object
}
這從上面輸出我,但然後繼續進入else塊,做什麼,我打算用一個有效的對象做。
任何人都可以發現我在那裏做錯了什麼嗎?
謝謝!
在構造函數的第一行發現了另一個問題 – 2009-01-21 15:21:41