Problem with SQL query when calling a static function from another static function in the same class

I am new to PHP, but not new to programming. I have a strange problem. This is such a simple thing, and I feel the solution is simple, but I tried for hours with no luck.

I have a model class User that contains the following function:

public static function byUsername ($ username) {
    $ row = DB :: fetchOne ('SELECT * FROM users WHERE username =?', $ username);
    if (! is_null ($ row)) {
        return new User ($ row);
    }
    return null;
}

It works as expected everywhere, returning a User object, given the correct username. Except for the User class itself: when I call a function with User::byUsername('a_valid_username')or self::, from another static function further in the User class, the function DB::fetchOne()simply returns nullno errors or exceptions.

What am I missing?

+5
source share
1 answer

I do not see a problem inside the method byUsernam().

Run

echo $username;

or

print_r( debug_backtrace() );

at the beginning byUsernam()to learn more about the environment being called.

+1
source

All Articles