r/PHP 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 Upvotes

11 comments sorted by

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.

1

u/NewMagazine3329 1d ago

Excuse my lack of experience, I'll explain: 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 lend this check. And let's say that at the same time your partner adds a third function where the price is a percentage but not a check and 1 is added to this and then divided 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?

2

u/TheLastOperator 1d ago

I woult create an operation interface that defines a function signature that takes the object you are performing calculations on as a parameter. Then write a class that implements the interface for each new operation. All that remains is to call the correct class for each operation you are performing. Since it's implementing an interface, you can pass the class instance to the function that goes through your array. This is more a OOP question rather than a PHP question. Look for "design patterns", you will learn a lot ;)

1

u/thomasmoors 1d ago

Maybe give some calculation examples to make it easier to understand your goal.

1

u/chuch1234 1d ago

Well when should each function be called?

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

u/FlorianRaith 1d ago

This sounds like use case for Strategy pattern no? 

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

1

u/Pakspul 1d ago

Is the overall method the one you are looking for? CalculatePrice(), and this holds gross costs (x * y), and then it calculates discount (other sub function), and so for? Split responsibility where they are and have a higher abstraction method combine it to a whole.

3

u/dirtside 1d ago

You should probably be asking in /r/phphelp, as specified by the rules of this sub.