1

All,

I'm getting an error with the code below. Here is the error message I get:
Notice: Undefined variable: userDAO in C:\wamp\www\Projetv0.2\Model_User.php on line 15
and then
Fatal error: Call to a member function checkRecordExists() on a non-object in C:\wamp\www\Projetv0.2\Model_User.php on line 15

The relevant code is below. What I try to do with the code is have a unique class (DAO_DBrecord) to access several tables in a db. In the case below, I want to access my users table. To do that I have created a specific static function createUserDAO inside the DAO_DBrecord class that calls the constructor with the right table name, users. However, it doesn't work, and I can't figure why.

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
    }
?>

Thank you all for your help!

JDelage

1 Answer 1

2

That's not a problem with the static function. The problem is that PHP doesn't have an implicit $this. When you're referring to the member variable within the class (as you are in userInfoExists), you have to say $this->userDAO rather than just $userDAO.

Of course, all this assumes that the DAO_DBrecord class has or inherits a checkRecordExists function. If it doesn't, you're going to have other problems.

Sign up to request clarification or add additional context in comments.

1 Comment

Doh... Thank you. It's always the dumbest mistakes that get me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.