1

Sorry if is a dummy question but I'm not really clear with it. Is it a good practice to integrate factory pattern in an mvc using php. For example could I have the following library structure:

index.php
 --init
   --abstract factory class for models
 --models 
   --model extend factory class 
 --controllers
 --views
lgt
  • 1,024
  • 3
  • 18
  • 33
  • I don't see why not, but I'm interested to see what others say. You can do some pretty neat factory-style coding with PHP's [__call](http://www.php.net/manual/en/language.oop5.overloading.php#object.call). – The Maniac May 11 '12 at 21:28
  • I have been searching around the web but I haven't got till now a good example. Could you please provide me an example? – lgt May 11 '12 at 21:37
  • 1
    Perhaps this helps as an example? http://stackoverflow.com/a/10559506/1329367 not strictly mvc though – Mahn May 12 '12 at 14:32

1 Answers1

2

One thing to note here is that models are not supposed to extend from factories, instead you would normally use the factories directly, which in turn should return a model initialized. E.g. imagine you have a frozen pizza (model) and a oven (factory); rather than eating the frozen pizza directly, you use the oven first, from which in turn you get the actual edible pizza. For a very complex type of oven (read, if the object creation process involves a lot) the factory pattern makes sense.

Other than that, no reason not to use the pattern if you feel it is useful for your needs. I generally include the factories directly where I store the models aswell, since they both strongly depend on each other (as in, one would not work without the other), but that's just a matter of taste I guess.

One last tip: use patterns when you really think you need them, not because they sound fancy. If you think not using the factory pattern makes your life easier and will get your project done faster, then by all means don't use it. That's not to say you shouldn't explore new patterns, but always rationalize whether the time and effort to implement and maintain them is justified.

Mahn
  • 16,261
  • 16
  • 62
  • 78