php - PDO Wrapper Class - If Connected Check -


i'm writing pdo wrapper bit of learning project , i'm trying combine using dependency injection. current set has factory set dependencies. so, example, i'll have form class passes in couple of objects output html attributes etc.

i'm wanting keep in factory class, want use 1 instance of each object, instead of making new object everytime. example explain better:

class factory {  public static function form() {      $html = new html;     $form = new form;     $form->html = $html;     return $form;  }  } 

and html class:

class html {  // outputs attributes in given array public function attributes($attributes = array()) {      // create string of html attributes     foreach($attributes $key => $attribute):         $tags .= $key . '=' . $attribute . ' ';     endforeach;      return $tags;  }  } 

so basically, want create html object once in factory pass through form method, create form , allow access html methods.

i'm assuming have setup constructor in factory create html object? problem see everytime run factory::form, html object created new. there simple way around that? i.e. when factory used, check see if html object created, if so, use current one.

it's simple great.

i'm not sure if mean, factory class can accept parameters in form() method, like

class factory {   public function form(html $html = null){     if(is_null($html)){       $html = new html();     }    // etc. } 

and other question, in fact factory class needs no constructor. need call appropriate method only.

edit

i haven't realized form method static. think you'd need static field in class, like:

class factory {   public static $html;    public static function form() {      if(is_null($this->html)){        $this->html = new html;     }     $form = new form;     $form->html = $this->html;     return $form;   } } 

please note in solution every object created factory have same $html object!


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -