Skip to content

Nit/trivial: Fix Anonymous functions Example # 4 cart "add" method to support adding quantity to an existing product - #5949

Merged
jordikroon merged 2 commits into
php:masterfrom
willy-b:fix-add-method-of-checkout-example-to-support-adding-the-same-product-multiple-times
Oct 19, 2026
Merged

jordikroon merged 2 commits into
php:masterfrom
willy-b:fix-add-method-of-checkout-example-to-support-adding-the-same-product-multiple-times

Conversation

@willy-b

@willy-b willy-b commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Nit/trivial - feel free to ignore or accept, figured I'd share in case you are interested to take the update.

At https://web.archive.org/web/20261014211813/https://www.php.net/manual/en/functions.anonymous.php#:~:text=add($product,%20$quantity)

the add method of the shopping cart example was written:

public function add($product, $quantity)
{
    $this->products[$product] = $quantity;
}

The implementation does not match the name, and is more like "set",
as if one called add twice for the same product it would not increment the quantity but replace with the value it was last called with, e.g.:

$my_cart->add('butter', 1);
$my_cart->add('butter', 1);

would result in a quantity of 1 for butter instead of 2.

This commit fixes that to avoid distraction by a strange add method from the point of the example which is the getTotal method.

…ng quantity to an existing product

At https://web.archive.org/web/20261014211813/https://www.php.net/manual/en/functions.anonymous.php#:~:text=add($product,%20$quantity)

the add method of the shopping cart example is written:
```
public function add($product, $quantity)
{
    $this->products[$product] = $quantity;
}
```

The implementation does not match the name, and is more like "set",
as if one called add twice for the same product it would not increment the quantity but replace with the value it was last called with, e.g.:
```
$my_cart->add('butter', 1);
$my_cart->add('butter', 1);
```

would result in a quantity of 1 for butter instead of 2. This commit fixes that to avoid distraction from the point of the example which is the getTotal method.
Comment thread language/functions.xml Outdated
… an unused function from the Anonymous functions Example # 4

- Style/simplicity: use null coalescing added in PHP 7 ( https://github.com/php/doc-en/blob/00eb00696f9e9989ffa539be35571429b25333b8/appendices/migration70/new-features.xml#L115 ) instead of ternary, per php#5949 (comment) , in the context of fixing the "add" method to update existing quantities instead of acting as a "set" method

- Also additionally remove the unused getQuantity method ( https://github.com/php/doc-en/blob/00eb00696f9e9989ffa539be35571429b25333b8/language/functions.xml#L1266 ), a pre-existing part of the example which also uses ternary, instead of updating to null coalescing, as after reviewing, it is found to be unused
@jordikroon
jordikroon merged commit efd61ff into php:master Oct 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants