Re: Properly handling multiple constructors.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 3/24/10 11:06 AM, Richard Quadling wrote:
On 24 March 2010 15:51, Robert Cummings<robert@xxxxxxxxxxxxx>  wrote:
Yes you can do that also, you seemed to want constructors though :) As for
misused... variable parameters with mixed types is common enough that I
wouldn't call it misuse. It's succinct and solves a problem. Look at the
printf() family of functions.

As multiple constructors were a no-go, I wanted an alternative. As I'm
working on this, I'm getting feedback from other people. The connector
needs to be a singleton - this is currently missing from the original
app. Something I wasn't told! (Well, maybe someone may have mentioned
it once). The static "constructors" (for that is what they end up
doing) along with the appropriate singleton logic would seem to
suffice and be a perfect match.

Thanks for all your help.

I'm trying to learn ZF as well as port this unfinished app.

Regards,

Richard.

This actually sounds like a factory to me. You want to move some of that logic out of the pizza object entirely.

class PizzaHut {

  public function getByKey($key) {
    $properties = $this->lookupFavorite($key);
    return new Pizza($properties);
  }

  public function getByProperties($properties) {
    return new Pizza($properties);
  }

  public function getByDefinition($base, $toppings, $cheese) {
    $properties = array(
      'base' => $base,
      'toppings' => $toppings,
      'cheese' => $cheese,
    )
    return new Pizza($properties);
  }
}

$factory = new PizzaHut();
$pizza = $factory->getByKey("MyFavorite");
$pizza = $factory->getByProperties(...);

And so on. The Pizza class is now responsible for one thing: Being pizza. MAKING a pizza is not the pizza's job, it's PizzaHut's job. All of the methods of PizzaHut can be cleanly documented, and you can add more ways of making Pizza without having to modify the Pizza class, and all of them return a pizza that looks identical. You could even create a Domino's class, too, to build pizza in an entirely different way but still get a pizza when you're done.

You may want to go as far as making each of those properties a set method rather than a big array of properties, but I leave that as an exercise to the reader. (I grow less enamored with big, hard to document arrays every time I use them.)

--Larry Garfield

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux