But in practice, I'm having a difficult time figuring out how to do this with my obviously limited OOP experience and knowledge.
https://r.je/I also found the above article on that same website, but it's WAY over my head.
I've been using PHP for over 15 years, and have always had a hard time wrapping my head around OOP, and just when I think I'm starting to figure it out, I learn that I know even less than I thought. Where do you think i should go from here, learn more about factories? Adding a new dependency would still require editing a lot of code.
The only thing that solution makes easier is that I don't have to pass the dependencies is a specific parameter order. I don't really like passing a parameter array, just as I don't like to have a long number of parameters in the construct.
Another big difference - static classes are not flexible. When you need only a single instance then a static class might be okay but what if you need another instance one day?
With a normal class you simply instantiate another object and you are good to go, while with a static class you have a big problem. Passing needed objects around (as dependencies) is really a small price to pay for long-term ease of maintenance.
I'm re-doing some legacy PHP code and converting a lot of it from procedural to OOP.
So, what's wrong with setting things up the way he described?This makes you code clearer, less magical, self-documenting, reusable and you will receive useful error messages early on (like missing dependency in the constructor). Say you have a static database class, if anyone gains access to run PHP in your application you can easily say good bye to your database, whereas if you had passed your database object as dependency injection of the objects that are going to use it solely, then the application is much more restricted and secure. I have to admit most of it goes right over my head at this point.In theory I understand and see why classes full of static properties and methods are not good in the long run, and that dependencies should be passed into an object's constructor.With hidden dependencies it becomes hard to find out which class needs what - the larger the application the more this problem will be apparent.A large application built on static classes becomes messy.
But I thought there could be a simpler, "cleaner" way of doing this.