Access extending class functions within an extending class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Access extending class functions within an extending class

Post by ben.artiss »

Hello again, I'm still having a bit of a headache with OOP, but this time I can't access the functions instead of the variables! Any help would be appreciated!

I have a core class which loads two extending classes; each stored in public vars.

Code: Select all

# all files loaded before core instantiated
class core {
    public $sql, $user;
 
    public function __construct() {
        $this->sql = new sql;
        $this->user = new user;
    }
 
    public function clean($input) {
        ...
        return $cleaned;
    }
}
The sql class has a public escape function which I need to access from the user class.

Code: Select all

class user extends core {
    public function __construct() {
        if (isset($_POST['username']) && isset($_POST['password'])) {
            $this->login();
        }
    }
 
    private function login() {
        // THIS WORKS FINE
        $username = parent::clean($_POST['username']);
 
        // HERE'S THE PROBLEM, I CAN'T GET AT THE EXTENDING SQL CLASS' FUNCTIONS
        $sql = sprintf("SELECT * FROM `users` WHERE `username`='%s' LIMIT 1", parent::sql::escape($username));
 
        /*
        I've tried variations such as:
        core::sql::escape();
        core->sql->escape();
 
        and I've tried making $core available via $this->core
        and I've tried making $sql static in the core
        ...but nothing's working!
        */
    }
}
Am I once again perhaps taking the wrong approach or is this resolvable?

Thanks, Ben.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Access extending class functions within an extending class

Post by ben.artiss »

Sorry! Fixed it using 'public static $sql' - don't know why I couldn't get it to work in the first place! :banghead:
Post Reply