<?php
// Un panier d'achat simple, qui contient une liste de produits
// choisis et la quantité désirée de chaque produit. Il inclut
// une méthode qui calcule le prix total des éléments dans le panier
// en utilisant une fonction de rappel anonyme.
class Panier
{
const PRICE_BEURRE = 1.00;
const PRICE_LAIT = 3.00;
const PRICE_OEUF = 6.95;
protected $products = array();
public function add($product, $quantity)
{
$this->products[$product] = $quantity;
}
public function getQuantity($product)
{
return isset($this->products[$product]) ? $this->products[$product] :
FALSE;
}
public function getTotal($tax)
{
$total = 0.00;
$callback =
function ($quantity, $product) use ($tax, &$total)
{
$test = $this->$products;
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);
}
}
$mon_panier = new Panier;
// Ajout d'élément au panier
$mon_panier->add('beurre', 1);
$mon_panier->add('lait', 3);
$mon_panier->add('oeuf', 6);
// Affichage du prix avec 5.5% de TVA
print $mon_panier->getTotal(0.055) . "\n";
Warning: Undefined variable $products in /in/TI7jn on line 34
Warning: Undefined property: Panier::$ in /in/TI7jn on line 34
Warning: Undefined variable $products in /in/TI7jn on line 34
Warning: Undefined property: Panier::$ in /in/TI7jn on line 34
Warning: Undefined variable $products in /in/TI7jn on line 34
Warning: Undefined property: Panier::$ in /in/TI7jn on line 34
54.54
Notice: Undefined variable: products in /in/TI7jn on line 34
Notice: Undefined property: Panier::$ in /in/TI7jn on line 34
Notice: Undefined variable: products in /in/TI7jn on line 34
Notice: Undefined property: Panier::$ in /in/TI7jn on line 34
Notice: Undefined variable: products in /in/TI7jn on line 34
Notice: Undefined property: Panier::$ in /in/TI7jn on line 34
54.54
Output for 7.3.32 - 7.3.33
54.54
Output for 7.0.0 - 7.0.20
Notice: Undefined variable: products in /in/TI7jn on line 34
Fatal error: Uncaught Error: Cannot access empty property in /in/TI7jn:34
Stack trace:
#0 [internal function]: Panier->{closure}(1, 'beurre')
#1 /in/TI7jn(40): array_walk(Array, Object(Closure))
#2 /in/TI7jn(53): Panier->getTotal(0.055)
#3 {main}
thrown in /in/TI7jn on line 34
Process exited with code 255.
Notice: Undefined variable: products in /in/TI7jn on line 34
Fatal error: Cannot access empty property in /in/TI7jn on line 34
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Fatal error: Using $this when not in object context in /in/TI7jn on line 34
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/TI7jn on line 32
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/TI7jn on line 32
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/TI7jn on line 10
Process exited with code 255.
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/TI7jn on line 10
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/TI7jn on line 10
Process exited with code 255.