Should a custom PHP class extend a database class?

I'm not sure if this is completely wrong, so I'm looking for some advice.

I created a database class with a constructor that establishes a PDO connection to a MySQL database.

I watched singletons and global variables, but it always seems like someone recommends against / or.

I am experimenting with a user class that extends a database class, so I can call PDO functions / methods, but maintain a separate user class code. Is this a stupid thing?

+6
source share
5 answers

Usually, you should pass the connection to your user, so your custom class will accept the database type object in its constructor, and then use this database object to make database queries. Thus, your data access logic remains separate from your business logic. This is called composition, unlike what you say is inheritance.

If you really wanted to be technical, it would be better to have a user object with nothing but public variables, and then you would use a “service” to implement your business logic.

class UserService implements IUserService { private $_db; function __construct(IDb $db) { $this->_db = db; } function GetAllUsers() { $users = Array(); $result = $this->_db->Query("select * from user") foreach($result as $user) { //Would resolve this into your user domain object here users[] = $user; } return users; } } 
+3
source

Well, ask yourself if User special case of Database . I'm not sure how others perceive this, but I would be offended. I think you need to read about the principle of Liskov’s signature .

As for the solution to your “people tell me problems with global errors”, here are two videos you should watch:

+2
source

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) { //some code... $this->db = $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; //some code... $this->db = $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.

+2
source

It is quite simple according to the definition of an object. This is data encapsulation and the operation performed on this data, so if we consider only the theoretical point of view, it will lead us into a pleasant environment.

My suggestion would be to create an abstract data access class with generalized crud basic operations and simple query execution using PDO, ADO or another database abstraction library. Now use this class as a parent for most of your model classes, such as User.

Now the basic CRUD is provided by the abstract data access class, and you can write user-specific behavior, for example, receiving all messages for the user, using the simple request interface of the abstract parent class.

This approach will bring greater modularity in terms of communication functionality and greater readability and reusability.

0
source

I do not see anything wrong with this for specific cases. You can use it for something as simple as transferring the credentials of a user database to an object, so they don’t need to specify them wherever the database object is used.

 $db = new UserDB(); 

will be a little better than

 $db = new StandarDB($username, $password, $default_db); 
-5
source

Source: https://habr.com/ru/post/899642/


All Articles