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? (30 h 09 min)

You also get:

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

Already a member? Login here

Comments & Discussion

V
Vadim ✓ Link copied!

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?

F
F_r ✓ Link copied!

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 ?

PK
Povilas Korop ✓ Link copied!

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

F
F_r ✓ Link copied!

Alright Povilas Korop.Thanks

P
Polfo ✓ Link copied!

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, ....

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.