Hi All
I'm a brand spanking noobie to PHP but here's what I'm up to...
I have a class that contains an array of instances of another class and I'm wondering whether it's possible to convert this array into a further class that contains methods.
Example:
Setup like this:
And later used like this:
Producing:
A Margarita pizza has:
Cheese
Tomato
If you haven't yet realised this is a highly simplified version of my code!! What I'm wondering is if there is any way of storing the Toppings array property of the Pizza class as another class (Class Toppings) rather than an array so that I'm able to define methods for the array such as:
Hope this makes some sense but please let me know if not!
Thanks
~S~
I'm a brand spanking noobie to PHP but here's what I'm up to...
I have a class that contains an array of instances of another class and I'm wondering whether it's possible to convert this array into a further class that contains methods.
Example:
PHP:
Class Topping{
private $sID;
private $sName;
public function ID($pID){
if(empty($pID)){
return $this->sID;
}else{
$this->sID=$pID;
};
}
public function Name($pName){
if(empty($pName)){
return $this->sName;
}else{
$this->sName=$pName;
};
}
}
Class Pizza{
private $sID;
private $sName;
private $sToppings;
public function AddTopping($ID,$Name){
$Topping=new Topping();
$Topping->ID=$ID;
$Topping->Name=$Name;
$this->Toppings['Topping_'.$ID]=$Topping;
}
public function ID($pID){
if(empty($pID)){
return $this->sID;
}else{
$this->sID=$pID;
};
}
public function Name($pName){
if(empty($pName)){
return $this->sName;
}else{
$this->sName=$pName;
};
}
public function Topings($pTopping){
if(empty($pTopping)){
return $this->sTopping;
}else{
$this->sTopping=$pTopping;
};
}
}
Setup like this:
PHP:
$myPizza=new Pizza();
$myPizza->ID=1;
$myPizza->Name='Margarita';
$myPizza->AddTopping(1,'Cheese');
$myPizza->AddTopping(2,'Tomato');
And later used like this:
PHP:
echo 'A '.$myPizza->Name.' pizza has: <br />';
foreach($myPizza->Toppings as $Topping){
echo $Topping->Name.';<br />';
};
Producing:
A Margarita pizza has:
Cheese
Tomato
If you haven't yet realised this is a highly simplified version of my code!! What I'm wondering is if there is any way of storing the Toppings array property of the Pizza class as another class (Class Toppings) rather than an array so that I'm able to define methods for the array such as:
PHP:
$Number_Of_Toppings=$Pizza->Toppings->Count;
$Pizza->Toppings->Add('Peppers');
Hope this makes some sense but please let me know if not!
Thanks
~S~