note 78663 added to language.oop
| From: | me at igorthetroll dot com | Date: | Mon, 22 Oct 2007 14:01:42 +0000 |
| Subject: | note 78663 added to language.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131629@lists.php.net to get a copy of this message | ||
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 -- https://master.php.net/note/delete/78663/integrated
Del: useless -- https://master.php.net/note/delete/78663/useless
Del: bad code -- https://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 -- https://master.php.net/note/delete/78663/in+docs
Del: other reasons-- https://master.php.net/note/delete/78663
Reject -- https://master.php.net/note/reject/78663
Search -- https://master.php.net/manage/user-notes.php