Comments

This profile is from a federated server and may be incomplete. Browse more on the original instance.

abhibeckert, to php in Understanding the Strategy Design Pattern in PHP with a Simple Example

Yeah I assume the same, but I’d really like to see a concrete example of that being done instead of having to guess (as someone who has never used the Strategy Design Pattern).

abhibeckert, to php in Hack — Enhancing PHP with Static Typing and Modern Features

This. Can people please stop posting medium articles?

Medium goes against basically everything Lemmy/etc stands for. It’s also just a shitty experience, for example code is rendered in a large font and cropped a 650 pixels without any wrapping. Yuck.

abhibeckert, (edited ) to php in Money pattern in PHP: the problem

I recommend going further than this example and don’t just store the value/currency for your price. You should also store the VAT. And generally anything else that involves division or percentages.

Jump through whatever hoops are necessary to work exclusively with addition and subtraction (and the occasional cheeky multiplication to shortcut repeated addition operations). Percentages and division is where people tend to get into trouble.

Specifically you should avoid the last line of code in this example from the article OP posted:


<span style="color:#323232;">echo $money->plus('4.99'); // USD 54.99
</span><span style="color:#323232;">echo $money->minus(1); // USD 49.00
</span><span style="color:#323232;">echo $money->multipliedBy('1.999'); // USD 99.95
</span><span style="color:#323232;">echo $money->dividedBy(4); // USD 12.50
</span>

I’m not sure what they were trying to get at with that example, so here’s a more realistic example where of avoiding percentages:


<span style="color:#323232;">class Product {
</span><span style="color:#323232;">    /**
</span><span style="color:#323232;">     * The price of the product excluding tax. Entered by the user into the database.
</span><span style="color:#323232;">     */
</span><span style="color:#323232;">    public Money $price;
</span><span style="color:#323232;">
</span><span style="color:#323232;">    /**
</span><span style="color:#323232;">     * The amount of VAT to collect for this product. Not entered by the user, but calculated
</span><span style="color:#323232;">     * whenever the product price changes (or, whenever the tax legislation changes). The
</span><span style="color:#323232;">     * value is stored in the database as an integer (and currency).
</span><span style="color:#323232;">     */
</span><span style="color:#323232;">    public Money $vat;
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">class CartItem {
</span><span style="color:#323232;">    public Product $product;
</span><span style="color:#323232;">    public int $qty;
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">$invoiceTotal = new Money(0);
</span><span style="color:#323232;">$invoiceVat = new Money(0);
</span><span style="color:#323232;">$paymentAmount = new Money(0);
</span><span style="color:#323232;">
</span><span style="color:#323232;">foreach ($cartItem in $cart) {
</span><span style="color:#323232;">    $invoiceTotal
</span><span style="color:#323232;">        ->add($cartItem->product->price)
</span><span style="color:#323232;">        ->multiply($cartItem->qty);
</span><span style="color:#323232;">    
</span><span style="color:#323232;">    $invoiceVat
</span><span style="color:#323232;">        ->add($cartItem->product->vat)
</span><span style="color:#323232;">        ->multiply($cartItem->qty);
</span><span style="color:#323232;">
</span><span style="color:#323232;">    $paymentAmount
</span><span style="color:#323232;">        ->add($cartItem->product->price)
</span><span style="color:#323232;">        ->add($cartItem->product->vat)
</span><span style="color:#323232;">        ->multiply($cartItem->qty);
</span><span style="color:#323232;">}
</span>

Avoiding division should also be done everywhere else - for example if your credit card facility charges 30c + 2.9%… don’t fall for the trap of calculating 2.9% of $paymentAmount because chances are you’ll round something in the wrong direction. Also, some cards have higher fees. Instead, when you authorise the payment the credit card facility should tell you that the card fee for that actual transaction is 152 cents. Record that number in your payment record and use it for your own internal reporting on profits/etc.

abhibeckert, to php in Understanding the Strategy Design Pattern in PHP with a Simple Example

<span style="color:#323232;">class SendVerificationEmail extends Notification {}
</span><span style="color:#323232;">class SendAnnouncementFCM extends Notification {}
</span><span style="color:#323232;">class SendOtpSMS extends Notification {}
</span>

What’s the point of those three classes? Why wouldn’t you just use Notification directly?

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • meta
  • Macbeth
  • All magazines