php - Object Factories -


i'm little overwhelmed managing complex php oop application. in past i've used static methods 'utils' classes i'm writing new app , want code highest standard possible i'm avoid them possible. maintain testability etc.

i've looked @ dependency injection i'm planning create 'library' (so-to-speak) of helper classes can drop in-and-out of projects without trouble. problem that, because i'll have maybe 5 or 6 of these, don't want set constructs in user needs pass of these objects in to.

my research has brought me factories - , i've created such beast don't know if right way go things. goes little this...

class create {  private static $validation = null; private static $helper = null; private static $html = null; private static $form = null; public static $user = null; public static $db = null;  // -------------------------------------------------------------- // initialize // --------------------------------------------------------------  public static function load($object, $options, $dependencies = array('html', 'helper', 'db', 'user')) {      // create specified object (without constructor)     // php version < 5.4     $$object = self::createinstancewithoutconstructor($object);      // inject specified options new object     foreach($dependencies $dependency):         if(is_null(self::$$dependency)): self::$$dependency = new $dependency; endif;         $$object->$dependency = self::$$dependency;     endforeach;      // call constructor     // php version < 5.4     if(method_exists($$object, '__construct')):         $$object->__construct($options);     endif;      return $$object;  }  // -------------------------------------------------------------- // create instance of object without calling it's constructor // -------------------------------------------------------------- // workaround php version < 5.4 // updated use // reflectionclass::newinstancewithoutconstructor // when 5.4 more freely supported // --------------------------------------------------------------  private static function createinstancewithoutconstructor($class) {      $reflector = new reflectionclass($class);     $properties = $reflector->getproperties();     $defaults = $reflector->getdefaultproperties();      $serealized = "o:" . strlen($class) . ":\"$class\":".count($properties) .':{';     foreach ($properties $property){         $name = $property->getname();         if($property->isprotected()){                 $name = chr(0) . '*' .chr(0) .$name;             } elseif($property->isprivate()){                 $name = chr(0)  . $class.  chr(0).$name;             }             $serealized .= serialize($name);             if(array_key_exists($property->getname(),$defaults) ){                 $serealized .= serialize($defaults[$property->getname()]);             } else {                 $serealized .= serialize(null);             }         }     $serealized .="}";      return unserialize($serealized);  }  // -------------------------------------------------------------- // create user // --------------------------------------------------------------  public static function user($options = array()) {      $user = self::load(__function__, $options);     return $user;  }  // -------------------------------------------------------------- // create page // --------------------------------------------------------------  public static function page($options = array()) {      $page = self::load(__function__, $options);     return $page;  }  // -------------------------------------------------------------- // create form // --------------------------------------------------------------  public static function form($name, $method = 'post', $action = null, $attributes = array()) {      // check see if form submitted     // if so, form object, otherwise create new form object     if(isset($_post[$name])):         $form = unserialize($_session['formobj']);         $form->errors = array();         $form->rule = $form->rule;         $form->labels = $form->labels;         $form->errors = $form->errors;     else:         $form = self::load(__function__, array(), array('html', 'validation'));         $form->name = $name;     endif;      // open form     $form->open($method, $action, $attributes);      return $form;  }  } 

so has separate functions creating various objects, there default selection passed in unless dependencies explicitly set.

now, i'm not sure if approach, more research, i've gathered may better idea have 1 factory each class responsible creating objects in class. more 'correct' way of approaching this? if so, should factory methods (not sure if that's right term?) statics can called anywhere or can normal methods creates new objects , pass them in? need extend 'factory' class holds created objects?

hope made sense, , appreciated.

yours,

confused n00b.

looking @ code , reading post, feel over-complicating things.

you don't need focus on "correct" way something. correct way way works best given scenario. @ need do, ahead future (but not far!!), , try find solution isn't over-designed, gives room grow.

i not far future because developers tend caught in design, trying plan every possible eventuality, , end monster impossible maintain.

if series of factory functions contained in static class working you, , don't foresee major issues time soon, why need keep designing? 1 of hardest things learn walk line between designing future, , getting things done :)


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -