Jump to content
php.lv forumi

Ko sevī glabā šis mainīgais?


L4URO
 Share

Recommended Posts

 Čau,

 

Tā tad,cik saprotu,tad $_instance mainīgais DB klasē ir DB instance,kad funkcija tiek izsaukta,kam pieejamas visas DB klases funkcijas.Kad tiek izsaukts  self::$_instance = new DB();    tad aiziet  __constructor metode un $_instance tiek pievienots _pdo, tad šis mainīgais tiek atgriezts. Es pareizi tik tālu sapratu? Un tad,kad kādā mainīgajā raksta $user = DB::getInstance(); , tad $user praktiski ir vienāds ar $_instance mainīgo?    Un ja raksta  $user = DB::getInstance()->query(); ,tad query funkcija pieder $user mainigajam vai $_instance metodei?        

$user = DB::getInstance();
<?php 
class DB {
  private static $_instance = null;
  private $_pdo, 
          $_query,
          $_error=false,
          $_results,
          $_count=0;
    
   private function __construct () {
	     try {
	         $this-> _pdo = new PDO('mysql:host='.Config::get('mysql/host').';dbname='.Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
	     }catch(PDOException $e){
		     die($e->getMessage());
	     }
   }       
   
   //singleton,the instance can be created only once
   public static function getInstance(){
	     if (!isset(self::$_instance)) {
	     	self::$_instance = new DB();
	     }
	     return self::$_instance;
   }
}

Paldies ,ja kaut ko nesaprotat, tad prasat!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...