Skip to main content

O: Open-Closed Principle - Eloquent Attribute with Salary Calculator

Premium
4:04

The Full Lesson is Only for Premium Members

Want to access all of our courses? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Vadim avatar

It's getting a bit tricky. You have created App/Calculators folder with calculator files/classes in it. How you would call them - is it Actions, is it Helpers? What are those classes?

👍 1
F_r avatar

Instead of having an associative array matching the position and the corresponding class, can we not use the interface? I think we'll have somthing like this

public function getSalaryAttribute(SalaryCalculatorInterface $salarayCalculator) {
 
//And then for any type of position, we'll juste have to do
$salarayCalculator->calculate($this->start_date)
 
}

What do you think ?

Povilas Korop avatar

Hmm, I think it's quite a good idea, can't answer 100% without trying it out but I think that would work.

👍 1
F_r avatar

Alright Povilas Korop.Thanks

Polfo avatar

I really don't like this example. If you want to check the consistency of your salary policy, you have to open a file for each employee type and layout them in such a way that you have an overview. Having all in one file violates the open-closed principle but is much faster to read, check, compare, ....