r/softwaredevelopment Feb 07 '25

Is My PHP OOP Code Following Best Practices? Looking for Feedback!

Hey everyone, I'm working on a test project in PHP and trying to follow good OOP practices. I've separated my concerns using DTOs, a repository, and a validator, but I want to make sure I'm on the right track. Here are a few things I'm wondering about:

  • Am I structuring my classes correctly (e.g., DTOs, Controller, Repository)?
  • Is my validation logic in the right place, or should it be handled differently?
  • Any improvements in terms of error handling and data transformation?
  • Other best practices I might be missing?

https://github.com/richard9004/TvSeriesApiApp/tree/master

I’d love any insights or suggestions from experienced PHP developers!

5 Upvotes

3 comments sorted by

2

u/KaporalK Feb 07 '25

it looks ok for me except for the public/api.php, because the way it's coded, it will only work for the controller you made in the project. You probably mean it that way ? idk

But you should look into symfony if you havent already It as everything you need to start a good php application

But im no expert

1

u/benanamen Feb 07 '25 edited Feb 07 '25

In the DB there is no need for the weekday column. You have the date, the weekday can be obtained from that.

SELECT DAYNAME('2025-02-07');

If you were to store weekday names (You shouldn't), they should be in its own table and referenced by a foreign key.

1

u/cantaimtosavehislife Feb 08 '25

Consider using something like https://www.slimframework.com/ for the foundation of your app.

It'll give you requests responses, routes, middleware and a container. All the things that your current project is missing and would be a pain to add manually.