I'm back again with another question
I have just started learning about OOP in PHP and am testing out what i can do with it
i made a simple little shopping cart with the folowing code
Code: Select all
<?php
CLASS store{
public $cart=array();
Public $R;
Public $X="apples";
Public $T="oranges";
Function __construct(){
echo "<h1 align='center'>Welcome to the $name</h1>";
}
Function buy_oranges($count){
$this->cart[]="$count $this->T";
}
Function buy_apples($count){
$this->R="You've bough $count " . $this->X;
$this->cart[]=("$count $this->X");
}
Function add_to_cart(){
echo" this is your cart";
$cart=array($this->R);
print_r($cart);
}
Public Function view(){
echo"<h3><font color='blue'>This Is your Cart</font></h3>";
FOREACH($this->cart AS $item){
echo"$item <br>";
}
}
}
$freds_corner_store=new store("Freds Corner store");
$freds_corner_store->buy_apples(4);
$freds_corner_store->buy_oranges(22);
$freds_corner_store->buy_apples(10);
$freds_corner_store->view();
?>Code: Select all
[size=150][b]Welcome to the[/b][/size]
[color=#0000FF][b]This Is your Cart[/b][/color]
4 apples
22 oranges
10 applesCode: Select all
<?php
CLASS cart EXTENDS store{
public function __construct(){
}
Public Function view(){
echo"<h3><font color='blue'>This Is your Cart</font></h3>";
FOREACH($this->cart AS $item){
echo"$item <br>";
}
}
}
$freds_corner_store=new store("Freds Corner store");
$my_cart=new cart();
$freds_corner_store->buy_apples(4);
$freds_corner_store->buy_oranges(22);
$freds_corner_store->buy_apples(10);
$my_cart->view();
?>Code: Select all
[size=150]Welcome to the[/size]
[color=#0000FF][b]This Is your Cart[/b][/color]i know this can work. i just don't know what i need to do/change/add to make it work.
Any Suggestion?
Thanks a Million
Oliver