The idea of extending a class in OOP is for child classes to be associated with parent classes. For example, a school may have a Person class with faculty extension classes and students. Both child classes are people, so it makes sense to extend the Person class. But the user is not a type of database, so some people may be upset if you make this extension.
Personally, I would send the database object as an argument to the User class in the constructor and simply assign this object to the class property. For instance:
class User { protected $db; function __construct($username, $password, $db) {
Alternatively, although some may yell at you for this, you can use the global keyword to inherit a variable in the global scope for use in your methods. The disadvantage is that you would have to declare it global in every method that it needs, or you could do:
class User { protected $db; function __construct($username, $password) { global $db;
But in answer to your question no, I do not think that you should make the user an extension of the database; although it will do what you need, it is not good OOP practice.
source share