2017-02-23 131 views
2

一個成員函數查詢()我是一個新手,OOP和尚在測試階段,能有人指出哪裏我的錯誤是什麼? 我收到提示致命錯誤:未捕獲的錯誤:調用上的空

Fatal error: Uncaught Error: Call to a member function query() on null on the following line:

我已經意識到,DB連接沒有被從核心類繼承,但我不知道爲什麼。

$user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1"); 

class.php

class Core { 
     public $db; 
     public $domain; 
     public $settings; 
     public function getDomain() { 
      return $this->domain; 
     } 
     function __construct($db,$domain) { 
      $this->db = $db; 
      $this->domain = $domain; 
      $this->getSettings(); 
     } 
     public function getSettings() { 
      $settings = $this->db->query("SELECT * FROM settings"); 
      $settings = $settings->fetch_object(); 
      $this->settings = $settings; 
     } 
    } 

    class User extends Core { 
     public $user; 
     function __construct($user_id) { 
      $this->setUser($user_id); 
     } 
     public function setUser($user_id) { 
      $user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1"); 
      if($user->num_rows >= 1) { 
       $user = $user->fetch_object(); 
       $this->user = $user; 
      } 
     } 

的index.php

$core = new Core($db,$domain); 
$user = new User($_SESSION['user_id']); 

澄清:

我的數據庫變量從我的config.php文件通過我包括index.php頁面的開始 的config.php

$_db['host'] = 'localhost'; 
$_db['user'] = 'root'; 
$_db['pass'] = 'root'; 
$_db['name'] = 'social'; 
$db = new mysqli($_db['host'], $_db['user'], $_db['pass'], $_db['name']) or die('MySQL Error'); 
+0

什麼時候開始的會議?該會話數組是否包含一個值? –

+0

是的,會議已經開始。我認爲這是事做不被繼承了$ DB變量或東西 –

+0

一個正在進行幾件事情。您可以在寫入$ core = new Core($ db,$ domain)之前展示如何設置變量$ db? – Yolo

回答

1

現在的問題就在於你調用類的方式中。 Core類__construct()函數需要$ db連接。現在,User類將不得不調用Core類的構造函數來啓動數據庫連接。所以,你有一個選擇至少:

呼叫只有用戶類是這樣的:

$user = new User($db,$domain,$_SESSION['user_id']); 

而改變你的用戶構造函數來做到這一點:

function __construct($db,$domain,$user_id) { 
    parent::__construct($db,$domain); 
    $this->setUser($user_id); 
} 

如果你的數據庫連接變量都是正確(主機/用戶名/密碼/端口),它應該工作。

1

如果構建一個子類,父類的子類的方法如果方法是直接

class Core { 
    public $db; 
    public $domain; 
    public $settings; 
    public function getDomain() { 
     return $this->domain; 
    } 
    function __construct($db,$domain) { 
     $this->db = $db; 
     $this->domain = $domain; 
     $this->getSettings(); 
    } 
    public function getSettings() { 
     $settings = $this->db->query("SELECT * FROM settings"); 
     $settings = $settings->fetch_object(); 
     $this->settings = $settings; 
    } 
} 

class User extends Core { 
    public $user; 
    function __construct($user_id,$db,$domain) { 
     parent::__construct($db,$domain); 
     $this->setUser($user_id); 
    } 
    public function setUser($user_id) { 
     $user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1"); 
     if($user->num_rows >= 1) { 
      $user = $user->fetch_object(); 
      $this->user = $user; 
     } 
    } 

稱爲不被使用和的index.php只是$user = new User($_SESSION['user_id'],$db,$domain);

0

沒有設置繼承後父類的構造。

例如

class Core { 
     public $db; 
     public $domain; 
     public $settings; 
     public function getDomain() { 
      return $this->domain; 
     } 
     function __construct($db,$domain) { 
      $this->db = $db; 
      $this->domain = $domain; 
      $this->getSettings(); 
     } 
     public function getSettings() { 
      $settings = $this->db->query("SELECT * FROM settings"); 
      $settings = $settings->fetch_object(); 
      $this->settings = $settings; 
     } 
} 

class User extends Core 
{ 
    public $user; 

    // parent:param Parameters add 
    function __construct($user_id,$db,$domain) { 
     // parent constructer call 
     parent::__construct($db,$domain); 
     $this->setUser($user_id); 
    } 
    public function setUser($user_id) { 
     $user = $this->db->query("SELECT * FROM users WHERE id='".$user_id."' LIMIT 1"); 
     if($user->num_rows >= 1) { 
      $user = $user->fetch_object(); 
      $this->user = $user; 
     } 
    } 
} 


$user = new User($_SESSION['user_id'],$db,$domain); 

這是不好嗎? http://php.net/manual/en/language.oop5.decon.php

相關問題