473,766 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ 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 withExtraToppin g implements Pizza{

private $cost = 0.50;
private $pizza;

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

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

$pizza = new Margherita();
$topping1 = new withExtraToppin g($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 1367
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****@mlynarc zyk-webdesign.dewro te:
>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','C heeseTopping' 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::getRecip e(),
Pizza::getBoxSi ze(), Pizza::getCalor ies() 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 withExtraToppin g implements Pizza{

private $cost = 0.50;
private $pizza;

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

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

$pizza = new Margherita();
$topping1 = new withExtraToppin g($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 withExtraToppin g($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 withExtraToppin g implements Pizza{

private $cost = 0.50;
private $pizza;

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

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

$pizza = new Margherita();
$topping1 = new withExtraToppin g($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 withExtraToppin g($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 withExtraToppin g 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*******@attgl obal.net
=============== ===

Oct 15 '07 #5
Jerry Stuckle schrieb:
Oliver Grätz wrote:
>$topping2 = new withExtraToppin g($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 withExtraToppin g 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 withExtraToppin g($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 withExtraToppin g 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*******@attgl obal.net
=============== ===

Oct 15 '07 #7
Jerry Stuckle schrieb:
Oliver Grätz wrote:
>Jerry Stuckle schrieb:
>>Oliver Grätz wrote:
$topping2 = new withExtraToppin g($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 withExtraToppin g 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 withExtraToppin g($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 withExtraToppin g 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*******@attgl obal.net
=============== ===

Oct 16 '07 #9

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

Similar topics

7
3697
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
2988
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 speed. They had a vague idea that PHP5 has improved handling of objects over PHP4, so it would probably be faster also. In fact it seems slower. We did a few timing loops, in which a a number of objects were created and and members were...
12
3623
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 <<<<<<<<<<<<<<< Instead of saying 3.23.57, shouldn't it show 5.xx.xx? Or, did I do something wrong?
1
15270
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 object handle. Waiting for the end of the script is aswell a bad option because the order in which the destructores are called does matter.
5
2321
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 are stability and security. What are the pros/cons/issues for PHP4/PHP5 with apache (either 1.3 or 2)? TIA,
0
1215
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 that I use called DBConn (this is the PHP5 ported definition for the members, PHP4 was the same except "private" was "var"): class DBConn { private $hDB; //db link private $bConnected; //db link status
0
1405
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 PHP5.1 on W2003 Server, ISAPI) I imported the whole project locally on a slightly more modern version of PHP 5.2.4 to make a few adjustments.
8
2320
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 constructors work seem to have changed quite a bit and I am not getting the same behavior across the versions. // Some simple code/
30
1911
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 some light on this! (code abbreviated for clarity) I have a parent class, DetailCollection, with a child class KeycodeTracker:
13
2410
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 representing students that referenced teacher objects. Then I happened to look at the temporary files created by sessions and found much redundant data was stored in each file about the teachers since they were referenced by every student in...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.