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.
----
Server IP: 219.94.145.73
Probable Submitter: 203.136.188.132
----
Manual Page -- http://
www.php.net/manual/en/language.oop.php
Edit -- https://master
.php.net/note/edit/78663
Del: integrated -- h
ttps://master.php.net/note/delete/78663/integrated
Del: useless -- http
s://master.php.net/note/delete/78663/useless
Del: bad code -- htt
ps://master.php.net/note/delete/78663/bad+code
Del: spam -- https:/
/master.php.net/note/delete/78663/spam
Del: non-english --
https://master.php.net/note/delete/78663/non-english
Del: in docs -- http
s://master.php.net/note/delete/78663/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78663
Reject -- https://mast
er.php.net/note/reject/78663
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|