全部,PHP呼叫靜態功能失敗
我收到以下代碼的錯誤。以下是錯誤消息我得到:
Notice: Undefined variable: userDAO in C:\wamp\www\Projetv0.2\Model_User.php on line 15
然後
Fatal error: Call to a member function checkRecordExists() on a non-object in C:\wamp\www\Projetv0.2\Model_User.php on line 15
相關的代碼如下。我試着用代碼做的是有一個獨特的類(DAO_DBrecord
)來訪問數據庫中的多個表。在下面的情況下,我想訪問我的users
表。爲此,我在DAO_DBrecord
類中創建了一個特定的靜態函數createUserDAO
,該類調用具有正確表名稱users
的構造函數。但是,它不起作用,我不明白爲什麼。
Model_User.php:
<?php
require_once('Class_DB.php');
require_once('DAO_DBrecord.php');
class Model_user{ // Represents a connection to the users table in the DB
private $db;
private $userDAO;
function __construct($db){
$this->db=$db;
$userDAO=DAO_DBrecord::createUserDAO($this->db);// static function - calls constructor w/ 'user' table name parameter
$this->userDAO=$userDAO;
}
function userInfoExists($userInfo, $colName){
return $userDAO->checkRecordExists($userInfo, $colName);
}
//Other stuff
}
?>
DAO_DBrecord.php:
<?php
require_once('Class_DB.php');
class DAO_DBrecord {
private $db;
private $table;
private function __construct($db,$table){
$this->db=$db;
$this->table=$table;
}
public static function createUserDAO($db) {
return new DAO_DBrecord($db, 'users');
}
//Other stuff
}
?>
謝謝大家的幫助!
JDelage
Doh ...謝謝。這總是讓我感到最愚蠢的錯誤。 – JDelage