note 78663 deleted from language.oop by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 19:02:58 +0000 |
| Subject: | note 78663 deleted from language.oop by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162552@lists.php.net to get a copy of this message | ||
Note Submitter: me at igorthetroll dot com
----
A little expanssion on the original shopping cart example.
It may look inherit to advance class user but to a beginer it will be helpful.
<?php
class Cart {
var $items; // Items in our shopping cart
// Add $num articles of $artnr to the cart
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
function remove_item($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
} elseif ($this->items[$artnr] == $num) {
unset($this->items[$artnr]);
return true;
} else {
return false;
}
}
function print_item($artnr) {
$blah=$this->items[$artnr];
return $blah;
}
}
?>
<?php
$cart = new Cart;
$cart->add_item("10", 5);
$cart->add_item("10", 3);
$cart->remove_item("10", 1);
echo $cart->print_item("10");
$another_cart = new Cart;
$another_cart->add_item("0815", 3);
?>
Added a print_item() function that returns the number of items in the shopping cart class for a
specific item, for later manipulation as a variable.