When you extend a class, the child class can call all the public and protected methods and properties of the parent class in addition to any the child defines. If you do not need to override the parent's methods, but just want to use them somewhere inside the child, use
public function somethingUsingExecute()
{
$query = $this->Execute("SELECT * FROM $table");
…
}
If you need to override execute
in a child class but also want to call that method with the original functionality of the parent, use
public function execute($table)
{
// can do something not in parent before parent call
$query = parent::Execute("SELECT * FROM $table");
// can do something not in parent after parent call
}
The static call
$query = DBManager::Execute("SELECT * FROM $table");
is something you should forget it exists, because you are hardcoding a classname into the using class and also couple the method call to the global scope. Both is hard to test and maintain. I won't go into more details, because this has been discussed on SO before. Just give it a search.
The "best trick" regarding that User/DBManager example would actually be not use inheritance at all. Inheritance creates an is-a relationship. The child is a special case of the parent. But a User is not a DBManager (I'd call it DbAdapter btw).
The responsibility of your DBManager class is to allow access to the database. Your User does not have that responsibility. It uses the DBManager to get access to the database, but it does need to know how to do that in detail. The DBManager already does.
Any class using another class should be injected/aggregated. So, what you should do is:
class User …
protected $db;
protected $isAuthenticated = FALSE;
public function __construct($db)
{
$this->db = $db;
}
public function authenticate($username, $password)
{
if($this->db->query('SELECT … ')) {
$this->isAuthenticated = TRUE;
}
return $this->isAuthenticated;
}
…
}
Then you can do
$db = new DBManager;
$john = new User($db);
$john->authenticate('JohnDoe', 'jd/12345');
$jane = new User($db);
$jane->authenticate('JaneDoe', 'jd/67890');
$logger = new DBLogger($db);
$logger->log('something to db');
Injecting the DB class has the added benefit that you can reuse the existing DBManager instance in other using classes. If you extend the DBManager class, each new instance will create a new connection to the database, which is undesired for performance reasons.