开发者

Using Singleton to create single MySQLi Connection and using it across multiple objects?

开发者 https://www.devze.com 2023-01-31 01:00 出处:网络
Goodmorning everybody! I\'m not completely new to PHP as in self but still kinda new when it comes to OO with PHP5. Now, I thought about this tiny project where I wanted to use HTML5 and some OO PHP.

Goodmorning everybody!

I'm not completely new to PHP as in self but still kinda new when it comes to OO with PHP5. Now, I thought about this tiny project where I wanted to use HTML5 and some OO PHP. It's supposed to be a horizontally scrollable timeline one day.

Therefore I created a class Timeline. This is my main object. Among other methods, one method ($timeline->createYearString();) is to create my string of years. I implemented another class Year which the method createYearString(); calls X number of times in order to create HTML output of my years.

In order to fill my years with facts, I need a database connection. Being new to the oop-approach towards mysql connections I search around and found Atli's comment (http://www.dreamincode.net/forums/topic/81496-connection-to-the-mysql-using-oop/page__view__findpost__p__1038512) about a mysqli singleton. I implemented it and tried using it in object of class Year. Funny thing, $dbLink gets returned for the first object but not the second one which is created instantly exactly the way the first one was. $this->db suddenly returns NULL in the second year and therefore I get a non-object FATAL error from PHP

See my three classes below. What am I missing?

class Year {
 private $year;
 private $db;

 function __construct($year) {
  $this->year=$year;
  $this->db=MyDB::Get();
 }

 function createHTML($last='') {
  $out=" <div class=\"year_outer".(($last) ? ' last_year' : '')."\" id=\"y".$this->year."\">
     <div class=\"year_inner\">
      <div class=\"year_above\">
       <span class=\"yearnr\">".$this->year."</span>
      </div>
      <div class=\"year_below\"></div>
     </div>
     <div class=\"months_wrapper\">\n";
  if(!$last) {
   for($i=1; $i<=12; $i++) {
    $fDoM = mktime(0,0,0,$i,1,$this->year);            // $fDoM = first Day of Month
    $lDoM = mktime(0,0,0,$i,date('t',mktime(0,0,0,$i,1,$this->year)),$this->year);  // $lDoM = last Day of Month
    $sql = $this->db->query("SELECT * FROM tp_together");
    //echo date('d.m.Y',$fDoM)." bis ".date('d.m.Y',$lDoM)."\n";

    $out.= " <div class=\"month_outer_l\" id=\"".$this->year."-".$i."\">
        <div class=\"month_outer_s\">
        开发者_JAVA技巧 <div class=\"month_inner\"><span>".date("M",mktime(0,0,0,$i,1,$this->year))."</span></div>
        </div>
       </div>\n";
    } 
  }  
  $out.= " </div>
    </div>\n";

  return $out;
 }
}

class Timeline {
 public $daysDating;
 public $startYear;
 public $endYear;
 public $numberOfYears;

 function __construct($start,$end='') {
  $this->startYear = $start;
  $this->endYear  = ($end=='') ? date("Y")+1 : $end;
  $this->daysDating = $this->calcDaysDating();
  $this->numberOfYears= $this->calcNumberOfYears();
 }

 function createYearString() {
  $x=new Year('2004');
  print $x->createHTML();
  $y=new Year('2005');
  print $y->createHTML();
  $z=new Year('2006');
  print $z->createHTML(true);

  // $j=$this->endYear;
  // for($i=$this->startYear; $i<=$j; $i++) {
  //  $x=new Year($i);
  //  ($i==$j) ?
  //   print $x->createHTML(true) :
  //   print $x->createHTML();
  // }
 }

 private function calcDaysDating() {
  $today=mktime(0,0,0,date("m"),date("d"),date("y"));
  $day1=mktime(0,0,0,7,11,2006);

  return $today-$day1;
 }

 private function calcNumberOfYears() {
  return $this->endYear - $this->startYear +1;
 }

 function createHNavigation() {
  $x=1;
  $out='<ul>';
  for($i=$this->startYear; $i<=$this->endYear; $i++) {
   $out.="<li><a class=\"navBubble\" id=\"navBubble$i\" accesskey=\"$x\" href=\"#y$i\" title=\"Go to $i\"></a></li>\n";
   $x++;
  }
  $out.="</ul>";
  return $out;
 }
}

class MyDB {
 private static $dbLink;

 private function __construct() {}
 private function __clone() {}

 public static function Get() {
  if(!self::$dbLink) {
   self::$dbLink = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);
   if(mysqli_connect_errno()) {
    throw new Exception("Database connection failed: ".mysqli_connect_error());
   }
   return self::$dbLink;
  }
 }
}

In case I didn't make my problem clear. $x resembles 2004 ($x=new Year('2004')) which works fine. But then the folling $y ($y=new Year('2005') returns the FATAL ERROR and $this->db returns NULL - WHY?

Thanks a lot, your help is greatly appreciated.


public static function Get() {
  if(!self::$dbLink) {
  self::$dbLink = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);
  if(mysqli_connect_errno()) {
   throw new Exception("Database connection failed: ".mysqli_connect_error());
  }
  //was here
  }
   return self::$dbLink; //should be here
 }


Your MyDB::Get function does not return self::$dbLink in the case where it already is set.

return in original:

public static function Get() {
    if(!self::$dbLink) {
        self::$dbLink = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);
        if(mysqli_connect_errno()) {
            throw new Exception("Database connection failed: ".mysqli_connect_error());
        }
        return self::$dbLink;
     }
}

return so that it works:

public static function Get() {
    if(!self::$dbLink) {
        self::$dbLink = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);
        if(mysqli_connect_errno()) {
            throw new Exception("Database connection failed: ".mysqli_connect_error());
        }
     }
     return self::$dbLink;
}

(PS, using more than a single character for an indent would make this a lot easier to detect.)

0

精彩评论

暂无评论...
验证码 换一张
取 消