Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Declare a basic shopping card that allows us to
- // add products to it, with a member that allows us
- // to get the quantity of a product we have added using
- // a callback function
- class Card
- {
- public static $products = Array();
- public static function add($product, $quantity)
- {
- self::$products[$product] = $quantity;
- }
- public static function getQuantity($product)
- {
- $quantity = false;
- array_map(function($value, $key) use($product, &$quantity)
- {
- if($key == $product)
- {
- $quantity = $value;
- }
- },
- array_values(self::$products),
- array_keys(self::$products)
- );
- return($quantity);
- }
- }
- // Add some items to our card
- Card::add('Butter', 1);
- Card::add('Milk', 3);
- Card::add('Eggs', 6);
- // Now get the total 'Eggs' we added
- printf('The card contains %d eggs', Card::getQuantity('Eggs'));
- ?>
Add Comment
Please, Sign In to add comment