Page 1 of 1

Class Extending problems

Posted: Fri Apr 10, 2009 2:06 pm
by enoc22
Hi all,
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();
?>
The out put is this

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 apples
i tried to make the view method and extension like so

Code: 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();
?>
but all i get back is

Code: Select all

[size=150]Welcome to the[/size]
[color=#0000FF][b]This Is your Cart[/b][/color]
i've tried using parent::_construct, no change.
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

Re: Class Extending problems

Posted: Fri Apr 10, 2009 3:12 pm
by Christopher

Code: Select all

$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();
?>
You are creating two separate objects. If you did $freds_corner_store->view() you would see the items you added it that object. Remember, and object is data with associated code that operates on that data. In your case $my_cart and $freds_corner_store each hold their own data.

Re: Class Extending problems

Posted: Fri Apr 10, 2009 3:53 pm
by enoc22
arborint- Thanks for the reply. i got it fixed up and it's working great now.
Thanks a million
Oliver

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);
        }   
 
}
CLASS cart EXTENDS store{
    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");
$freds_corner_store->buy_apples(4);
$freds_corner_store->buy_oranges(22);
$freds_corner_store->buy_apples(10);
/*------------------New Cart------------------------------------*/
$my_cart= new cart();
$my_cart->buy_oranges(22); //
$my_cart->view();
?>