samedi 18 août 2018

I'm having trouble understanding SOLID principles

I could really use some help with this one. I'm trying to use SOLID principles in my next php project but i'm having a really tough time understading it. First of all, i can't really grasp single responsibility principle.

Let's say i'm creating user registration. First, i will create Database class for connecting to database. Then, i will create Register class that will receive all the user input from form. I will create methods in Register class that will check the input and clean it. And now comes the hard part. Let's say i want to check if email already exist. I also need to check if username already exist. To do that, i need access to the database. So, i can pass my db connection to Register object. But, it seems to me that, if i follow SOLID principles, i shouldn't touch database in my Register class methods. I can't really understand why is it bad doing it that way?

I can create some DbHandler class that will handle queries, binding parameters and executing queries, and pass the information from Register object to it and then check if username and email already exist. But that, to me, looks unnecessary. And, for the Register class, here will be very few things to do.

Would it be good solution to create, let's say, some RegisterDB class, that will handle db interactions for Register object? But again, Register class would be doing just a few thing.

My head will explode, i could really use help with this, guys.

Aucun commentaire:

Enregistrer un commentaire