You are here: Re: PHP5 - Object « PHP Programming Language « IT news, forums, messages
Re: PHP5 - Object

Posted by Oliver Grδtz on 10/14/07 23:50

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]

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация