PHP5 - Object

This is a discussion on PHP5 - Object within the PHP Language forums, part of the PHP Programming Forums category; <?php interface Pizza { public function getPrice(); } class Margherita implements Pizza { private $cost = 4.50; public function getPrice(){ return $this-&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-14-2007
iavian
 
Posts: n/a
Default PHP5 - Object

<?php

interface Pizza {
public function getPrice();
}

class Margherita implements Pizza {

private $cost = 4.50;

public function getPrice(){
return $this->cost;
}
}

class withExtraTopping implements Pizza{

private $cost = 0.50;
private $pizza;

public function __construct(Pizza $pizza){
$this->pizza = $pizza;
}

public function getPrice(){
return $this->cost + $this->pizza->getPrice();
}
}

$pizza = new Margherita();
$topping1 = new withExtraTopping($pizza);
$total = $topping1->getPrice();
print $total;

?>

Need a couple of lines of code that would generate a margherita pizza
with two extra toppings from the classes above, and print out the
total cost of the pizza?

But how to create a pizza with 2 toppings ??

Reply With Quote
  #2 (permalink)  
Old 10-14-2007
Thomas Mlynarczyk
 
Posts: n/a
Default Re: PHP5 - Object

> But how to create a pizza with 2 toppings ??

I would do something like this:

interface NotForFree { function getPrice(); }
class Pizza implements NotForFree { ... }
class Margherita extends Pizza { ... }
class Topping implements NotForFree { ... }
class CheeseTopping extends Topping { ... ]
class OnionTopping extends Topping { ... }

$pizza = new Margherita;
$pizza->addTopping( new CheeseTopping );
$pizza->addTopping( new OnionsTopping );
$pizza->getPrice();

/* or */

$pizza = new Margherita( new CheeseTopping, new OnionsTopping );
$pizza->getPrice();

Greetings,
Thomas

--
C'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)


Reply With Quote
  #3 (permalink)  
Old 10-14-2007
Rik Wasmus
 
Posts: n/a
Default Re: PHP5 - Object

On Sun, 14 Oct 2007 15:42:25 +0200, Thomas Mlynarczyk
<thomas@mlynarczyk-webdesign.de> wrote:

>> But how to create a pizza with 2 toppings ??

>
> I would do something like this:
>
> interface NotForFree { function getPrice(); }
> class Pizza implements NotForFree { ... }
> class Margherita extends Pizza { ... }
> class Topping implements NotForFree { ... }
> class CheeseTopping extends Topping { ... ]
> class OnionTopping extends Topping { ... }
>
> $pizza = new Margherita;
> $pizza->addTopping( new CheeseTopping );
> $pizza->addTopping( new OnionsTopping );
> $pizza->getPrice();
>
> /* or */
>
> $pizza = new Margherita( new CheeseTopping, new OnionsTopping );
> $pizza->getPrice();


I'd definitely make Pizza a class and not an interface indeed. Then justa
properly executed Decorator Pattern can set, and tell you exactly what
type, toppings and total price this particular pizza would have. I would
not recommend making 'Margherita','CheeseTopping' etc. hardcoded classes..
Unless it remains static after development, it would be a maintenance
nightmare to add/remove/alter different kinds of pizza & toppings. I
assume come kind of interface that gives options for Pizza & Toppings
would be involved, which would mean that any alteration in those requires
altering code & data in several places.

Rather, I'd have a database with a Pizzas table, including name & price,
and a Toppings table, including name & price, and just a 'generic' Pizza
object with the type/name/price in a variable, and either 'decorated'
using the Decorator Pattern with generic Topping objects (which themselves
hold a toppingtype/toppingprice), or just an array of toppings in the
Pizza class itself, so you could do a:

class Pizza{
...
public function getPrice(){
$price = $this->_price;
if(!empty($this->_toppings){
foreach($this->_toppings as &$topping){
$price += $topping->getPrice();
}
}
return $price
}
...
}
... which hardly is any complicated pattern but gets the job done easily.
As long as 'toppings' don't have any profound impact on the 'Pizza' other
then price and actually being added as topping I'd choose this. If they
have other impacts, like changing the output of Pizza::getRecipe(),
Pizza::getBoxSize(), Pizza::getCalories() the Decorator Pattern would
certainly pay out.
--
Rik Wasmus
Reply With Quote
  #4 (permalink)  
Old 10-15-2007
Oliver Grätz
 
Posts: n/a
Default Re: PHP5 - Object

iavian schrieb:
> <?php
>
> interface Pizza {
> public function getPrice();
> }
>
> class Margherita implements Pizza {
>
> private $cost = 4.50;
>
> public function getPrice(){
> return $this->cost;
> }
> }
>
> class withExtraTopping implements Pizza{
>
> private $cost = 0.50;
> private $pizza;
>
> public function __construct(Pizza $pizza){
> $this->pizza = $pizza;
> }
>
> public function getPrice(){
> return $this->cost + $this->pizza->getPrice();
> }
> }
>
> $pizza = new Margherita();
> $topping1 = new withExtraTopping($pizza);
> $total = $topping1->getPrice();
> print $total;
>
> ?>
>
> Need a couple of lines of code that would generate a margherita pizza
> with two extra toppings from the classes above, and print out the
> total cost of the pizza?
>
> But how to create a pizza with 2 toppings ??
>


$topping2 = new withExtraTopping($topping1);
$total = $topping2->getPrice();
print $total;

Where's the problem, it's a simple decorator example.

OLLi

--
Piper: "See what I mean? We have bigger naked breasts to worry about."
Phoebe: "Paige has her naked breasts to worry about. I've got yours."
[Charmed 702]
Reply With Quote
  #5 (permalink)  
Old 10-15-2007
Jerry Stuckle
 
Posts: n/a
Default Re: PHP5 - Object

Oliver Grätz wrote:
> iavian schrieb:
>> <?php
>>
>> interface Pizza {
>> public function getPrice();
>> }
>>
>> class Margherita implements Pizza {
>>
>> private $cost = 4.50;
>>
>> public function getPrice(){
>> return $this->cost;
>> }
>> }
>>
>> class withExtraTopping implements Pizza{
>>
>> private $cost = 0.50;
>> private $pizza;
>>
>> public function __construct(Pizza $pizza){
>> $this->pizza = $pizza;
>> }
>>
>> public function getPrice(){
>> return $this->cost + $this->pizza->getPrice();
>> }
>> }
>>
>> $pizza = new Margherita();
>> $topping1 = new withExtraTopping($pizza);
>> $total = $topping1->getPrice();
>> print $total;
>>
>> ?>
>>
>> Need a couple of lines of code that would generate a margherita pizza
>> with two extra toppings from the classes above, and print out the
>> total cost of the pizza?
>>
>> But how to create a pizza with 2 toppings ??
>>

>
> $topping2 = new withExtraTopping($topping1);
> $total = $topping2->getPrice();
> print $total;
>
> Where's the problem, it's a simple decorator example.
>
> OLLi
>


Several things.

What's the difference between an object of class withExtraTopping and
$topping1?

What is the price of $topping2? Every pizza place I know charges more
for the same topping on a larger pizza.

I agree with Rik. Pizza is a real object, and should be a class, not an
interface.

Topping should also be a class. And keeping everything in a database
would be the best way to go.

And while you *could* create a class Margherita which extends Pizza, it
doesn't pay to get *too* specific. As Rik indicated, if you ever get
rid of this type of pizza or add others, you'll have to change a lot of
code.

Keep it simple. It makes life easier.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #6 (permalink)  
Old 10-15-2007
Oliver Grätz
 
Posts: n/a
Default Re: PHP5 - Object

Jerry Stuckle schrieb:
> Oliver Grätz wrote:
>> $topping2 = new withExtraTopping($topping1);
>> $total = $topping2->getPrice();
>> print $total;
>>
>> Where's the problem, it's a simple decorator example.
>>
>> OLLi
>>

>
> Several things.
>
> What's the difference between an object of class withExtraTopping and
> $topping1?
>
> What is the price of $topping2? Every pizza place I know charges more
> for the same topping on a larger pizza.
>


Changing the concept is out of scope here. I simply ANSWERED the
question asked by the original poster. Changing the price of extra
toppings and making the price dependent on the pizza size was NOT part
of the question.

OLLi

--
X:"I was working hard for that money."
S:"And I didn't?"
X:"You stole it."
S:"And you're making it very hard work"
[Buffy 514]
Reply With Quote
  #7 (permalink)  
Old 10-16-2007
Jerry Stuckle
 
Posts: n/a
Default Re: PHP5 - Object

Oliver Grätz wrote:
> Jerry Stuckle schrieb:
>> Oliver Grätz wrote:
>>> $topping2 = new withExtraTopping($topping1);
>>> $total = $topping2->getPrice();
>>> print $total;
>>>
>>> Where's the problem, it's a simple decorator example.
>>>
>>> OLLi
>>>

>> Several things.
>>
>> What's the difference between an object of class withExtraTopping and
>> $topping1?
>>
>> What is the price of $topping2? Every pizza place I know charges more
>> for the same topping on a larger pizza.
>>

>
> Changing the concept is out of scope here. I simply ANSWERED the
> question asked by the original poster. Changing the price of extra
> toppings and making the price dependent on the pizza size was NOT part
> of the question.
>
> OLLi
>


It is if you're emulating the real world.

Initial requirements do not always reflect final needs.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #8 (permalink)  
Old 10-16-2007
Oliver Grätz
 
Posts: n/a
Default Re: PHP5 - Object

Jerry Stuckle schrieb:
> Oliver Grätz wrote:
>> Jerry Stuckle schrieb:
>>> Oliver Grätz wrote:
>>>> $topping2 = new withExtraTopping($topping1);
>>>> $total = $topping2->getPrice();
>>>> print $total;
>>>>
>>>> Where's the problem, it's a simple decorator example.
>>>>
>>> Several things.
>>>
>>> What's the difference between an object of class withExtraTopping and
>>> $topping1?
>>>
>>> What is the price of $topping2? Every pizza place I know charges more
>>> for the same topping on a larger pizza.
>>>

>> Changing the concept is out of scope here. I simply ANSWERED the
>> question asked by the original poster. Changing the price of extra
>> toppings and making the price dependent on the pizza size was NOT part
>> of the question.
>>

>
> It is if you're emulating the real world.
>
> Initial requirements do not always reflect final needs.
>

Yes, but you were answering to MY RESPONSE and not to the original
poster. I just DIRECTLY answered the question. Other evangelizing
answers had already been given (and I didn't need to elaborate on them)
but I personally dislike the fact that these people tend to bring up
more questions INSTEAD OF solving the problem at hand. You can bring up
your own concept, fine. But please ANSWER THE QUESTION first. What
speaks against the idea that iavian just didn't get the concept of a
decorator? Nothing, since the code he already had was well capable of
solving the questions he asked without change!

OLLi

--
Never change a running system.
Reply With Quote
  #9 (permalink)  
Old 10-16-2007
Jerry Stuckle
 
Posts: n/a
Default Re: PHP5 - Object

Oliver Grätz wrote:
> Jerry Stuckle schrieb:
>> Oliver Grätz wrote:
>>> Jerry Stuckle schrieb:
>>>> Oliver Grätz wrote:
>>>>> $topping2 = new withExtraTopping($topping1);
>>>>> $total = $topping2->getPrice();
>>>>> print $total;
>>>>>
>>>>> Where's the problem, it's a simple decorator example.
>>>>>
>>>> Several things.
>>>>
>>>> What's the difference between an object of class withExtraTopping and
>>>> $topping1?
>>>>
>>>> What is the price of $topping2? Every pizza place I know charges more
>>>> for the same topping on a larger pizza.
>>>>
>>> Changing the concept is out of scope here. I simply ANSWERED the
>>> question asked by the original poster. Changing the price of extra
>>> toppings and making the price dependent on the pizza size was NOT part
>>> of the question.
>>>

>> It is if you're emulating the real world.
>>
>> Initial requirements do not always reflect final needs.
>>

> Yes, but you were answering to MY RESPONSE and not to the original
> poster. I just DIRECTLY answered the question. Other evangelizing
> answers had already been given (and I didn't need to elaborate on them)
> but I personally dislike the fact that these people tend to bring up
> more questions INSTEAD OF solving the problem at hand. You can bring up
> your own concept, fine. But please ANSWER THE QUESTION first. What
> speaks against the idea that iavian just didn't get the concept of a
> decorator? Nothing, since the code he already had was well capable of
> solving the questions he asked without change!
>
> OLLi
>


Yes, I responded to you because I thought your response was not a good
way to do it, for the reasons I gave.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:08 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0