WebDevStudios / oops-wp

A collection of abstract classes, interfaces, and traits to promote object-oriented programming practices in WordPress.
57 stars 9 forks source link

Refactor service initialization to convert index array to associative… #13

Closed jmichaelward closed 5 years ago

jmichaelward commented 5 years ago

Closes #11

@aubreypwd Here's a PR for you. This follows the process I'm using else where an array of services such as:

protected $services = [
    ClassOneName::class,
    ClassTwoName::class,
];

...becomes an array like:

protected $services = [
    '\ClassOneName' => {ClassOneName Instance},
    '\ClassTwoName' => {ClassTwoName Instance},
];

Please note that it is intentional that there is no getter in this class. I want OOPS to promote SOLID principles and dependency injection. If one of your services requires access to another service, that should be established by passing one object into another through some method or the Service constructor. That said, you're right that by caching the instantiated services to the class property, you'll be able to access those instances where needed. The run property is public and the register_services method is protected, so a concrete class can easily override this initialization procedure if there's a particular use case for it.

Let me know if you have any questions.