r/PHP • u/NewMagazine3329 • 1d ago
Different operations, a single function
Sorry, English is not my native language.
I started programming in PHP for my job and I saw something that I'm not sure how to do and that is adding a new mathematical formula. To give a little context I will say that we use a simple formula to calculate something (quantity * price), now we have added two new ways to calculate.
When you create a transaction, regardless of the type, it will go through a specific function, then the first formula and the 2 new ones will go through the same place, being and serving very different things. My question is, what is the best way to handle different mathematical operations that will go through the same controller and ensure future additions?
Edit: I'm going to add an example that I gave in the comments, I hope it helps with clarity, and also writes the post better. Sorry for the inconvenience, I'm still learning from being here:
Currently the total is calculated by multiplying quantity by price. Now a function has been added, let's say for the check calculation example, where you multiply the amount by the price but the latter for this operation will be a percentage that will be divided by 30 and finally multiplied by the days that you give to this check. And let's say at the same time your partner adds a third function where the price is a percentage but not a check and you add 1 to this and then divide by the amount. We have 3 formulas in this example, the original quantity per price and the 2 new ones.
What is the way to handle the different mathematical operations of a software ensuring that more can be added in the future?
0
u/New-Alfalfa-6641 1d ago
I guess you're asking about operator overload? Some languages (e.g. C#) let you redefine how * should work for custom types, but such feature is not available in PHP.
So * in PHP simply means multiplication. Similar operator ** means doing power operation, e.g. 2**3 means 2 to the power of 3.
1
1
u/equilni 1d ago
operations that will go through the same controller and ensure future additions?
You answered your own question here. The Controller could/should pass and receive information from the inner system (Model/Domain).
Controller {
...
(array/object) $totals = $calculationService->getTotals($quantity, $price);
}
To give a little context I will say that we use a simple formula to calculate something (quantity * price), now we have added two new ways to calculate.
At it simplest, you are just passing the Quantity and Price to whatever function/classes.
$quantity = (int);
$price = (int);
$total = calcTotalPrice($quantity, $price);
$totalSecondCalc = calcSecondTotal($quantity, $price);
$totalThirdCalc = calcThirdTotal($quantity, $price);
"Price" can be a separate entity itself. The system should designate pricing based on things like quantity or date (sale). Your calculation could just be $item & $quantity, but you are not noting a $item here...
let's say for the check calculation example
Are we talking about payment check? Will this always be a 1 item sale?
ensure future additions?
We have 3 formulas in this example, the original quantity per price and the 2 new ones.
How are these calculations being presented?
Probably a bigger example is needed.
1
u/Glittering-Baby2906 1d ago
i think you need to breakdown the variables that would affecting the calculations, and maybe use a pipeline pattern? where basically passing the "calculator class" into the array, where each one of them holding the variables, and always return the same thing (e.g. total), and it'll be pass through other defined calculator withtin the array.
im not sure if that makesense, the minus to this approach is, it'll get hard to debug because all the logic is behind each calculator class.
the example of this usage is in Laravel middleware definition
3
u/dirtside 1d ago
You should probably be asking in /r/phphelp, as specified by the rules of this sub.
6
u/chuch1234 1d ago
Sorry, this doesn't make any sense :/ You might want to explain in more detail. Start at the beginning with a good example. Use short sentences and don't skip anything.