Re: Properly handling multiple constructors.
| From: | larry@garfieldtech.com | Date: | Wed, 24 Mar 2010 16:48:37 +0000 |
| Subject: | Re: Properly handling multiple constructors. | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-303335@lists.php.net to get a copy of this message | ||
On 3/24/10 11:06 AM, Richard Quadling wrote:
On 24 March 2010 15:51, Robert Cummings<robert@interjinn.com> wrote: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) {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.
$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