473,467 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 ??

Oct 14 '07 #1
8 1354
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)
Oct 14 '07 #2
On Sun, 14 Oct 2007 15:42:25 +0200, Thomas Mlynarczyk
<th****@mlynarczyk-webdesign.dewrote:
>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
Oct 14 '07 #3
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]
Oct 15 '07 #4
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.
js*******@attglobal.net
==================

Oct 15 '07 #5
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]
Oct 15 '07 #6
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.
js*******@attglobal.net
==================

Oct 15 '07 #7
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.
Oct 16 '07 #8
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.
js*******@attglobal.net
==================

Oct 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christoph Nothdurfter | last post by:
Hallo! I was wondering if my PHP4-Scripts will run under PHP5 (Haeven't tried the beta yet). Does anybody know? Thank you, -Christoph
8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
12
by: Sarah Tanembaum | last post by:
Though I installed MySQL5 and PHP5, how come my phpinfo() shows as follow: MySQL Support enabled Active Persistent Links 0 Active Links 0 Client API version 3.23.57 <<<<<<<<<<<<<<< ...
1
by: Thomas Ilsche | last post by:
Hi, how do I "explicitly destroy" an Object in PHP5 to make sure the destructor is called an the object destroyed? unset is not an option because there are multiple variables containing the...
5
by: sinister | last post by:
I'm starting a database/web interface project, using Linux and postgresql. I've programmed in PHP4 in the past, and for this new project am unsure whether to use PHP4 or PHP5. My main concerns...
0
by: Tim Meader | last post by:
Hello, I was hoping someone could provide some insight as to the best approach to take in porting over a function I use in quite a few spots from PHP4 to PHP5. Basically, I have a db wrapper class...
0
by: Erwin Moller | last post by:
Hi group, I found something strange in PHP5.2.4 (on IIS7/Vista). I am working on an app that has been running just fine under heavy load for over a year at some custumer of mine. (they have...
8
by: FFMG | last post by:
Hi, I am slowly moving my code to php5. But I would like to make it backward compatible in case something bad happens, (and to make sure I understand what the changes are). The way the...
30
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
13
by: DigitalDave | last post by:
A project I did awhile back stored php5 objects in elements of the $_SESSION array between pages that were navigated on the site. There were object classes representing teachers, and object classes...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.