全部,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