0

I have a PHP class with a few functions defined, this class is responsible for database access:

class database {

    function open($params) {
        // code here to open the db
    }

    function close() {
        // code here to close the db
    }

    function count_users() {
        // code here counts the number of user records

        // Return -1 for testing
        return -1;
    }

    function insert_user($user) {
        // code here inserts a user record
    } 

    function select_user($user_id) {
        // code here selects a user record
    }

}

I have accessor classes defined as follows:

require_once("database.php");

class user {

    public $user_id;
    public $email_address;
    // etc, etc

}

class db_user {

    static function select_user($user_id) {
        $db = new database();
        $db->open();

        $user = NULL;

        $result = $db->select_user($user_id);

        // Test the result and decode user record into $user, etc

        $db->close();

        return $user;
    }

    static function count_users() {
        $db = new database();
        $db->open();

        $count = $db->count_users();

        $db->close();

        return $count;
    }

}

My issue occurs when I attempt to count the number of users through db_user::count_users(); which always fails with a Fatal Error: call to undefined method database::count_users

If I dump the database class methods using get_class_methods, I can see that the count_users function isn't present in the list but I have no idea why.

I'm very much a PHP n00b so there maybe something really obvious I'm not doing. My db_user and user classes have many other functions which pull data back through the database class and all of these succeed - just this one function.

Please help!


UPDATE

Ok, so, having removed a couple of functions from the database class and re-uploaded the file to the live server, it appears that it is somehow being "cached" as when I dump the methods belonging to the database object, the removed methods are still displayed in the list.

The count_users function is also not present in the method list yet when I inspect the file uploaded to the server, it is there in code.

Is there any way of removing this caching???

3
  • db_user doesn't have a method count_users this method is in class database you can extend class db_user or the best way is to use Dependency injection Commented May 14, 2015 at 10:43
  • The method does exist in the db_users class and in the database class - its the database one it complains about though. Commented May 14, 2015 at 10:45
  • @weblar83 please ark and up-vote the answer for others help. thanks. Commented Feb 3, 2016 at 18:31

4 Answers 4

1

Try as follows:

class user extends database {
   //code user class goes here
}

or

class user extends db_user {
   //code user class goes here
}

simple problem solved.

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

1 Comment

But why does the problem exist in the first place? As I mentioned, ALL the other functions in the 'db_user' class work except this one "count_users" function. Why? What is different about this one?
1

You can store you instance of Database to the variable.

class db_user {
    public static $db;
    static function openDatabase(){
        self::$db = new database();
        self::$db->open();
    }
    static function select_user($user_id) {

        $user = NULL;

        $result = self::$db->select_user($user_id);

        // Test the result and decode user record into $user, etc

        self::$db->close();

        return $user;
    }

    static function count_users() {

        $count = $db->count_users();

        self::$db->close();

        return $count;
    }

}

Comments

1

The issue, it would appear, is related to another version of the "database.php" file hiding in a sub-folder which must have been copied there by mistake.

Having removed this troublesome file, all now works as expected.

Thanks for your help.

Comments

0

Tried running your code in on-line phptester tool - first complained about missing $params in $db->open(), removing the argument (or passing the $params) it works fine on php 5.2,5.3,5.4. No complaints about count_users().

2 Comments

I've just tried running my code on my local ZWAMP server and it works absolutely fine, without issue. If I try running the same code on the live server, that's when the issue occurs - maybe a difference in PHP version?
On my local machine I'm running PHP 5.4.12 but on the live site, its running 5.4.39...

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.