當我運行我的代碼時,我收到錯誤訊息Fatal error: Uncaught Error: Call to a member function run() on null in C:\xampp\htdocs............. 我有三個類關于我的問題
- 在里面
- 實用程式
- 撤銷初始化類持有資料庫連接
class Init{
private $pdo;
function __construct(){
$servername = "localhost";
$username = "root";
$dbName = "**************";
$password = "";
try {
$dsn = "mysql:host=".$servername.";dbname=".$dbName;
$this->pdo = new PDO($dsn, $username, $password);
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->pdo;
} catch (Exception $e) {
echo $e->getMessage();
}
}
public function run($sql, $args = NULL)
{
$stmt=null;
if (!$args)
{
return $this->pdo->query($sql);
}
try {
$stmt = $this->pdo->prepare($sql);
$stmt->execute($args);
} catch (PDOException $e) {
//SEND THIS TO AUDIT SERVICE FOR LOGGING
echo $e->getMessage();
}
return $stmt;
}
}
公用事業類的橫截面
require 'init.php';
class Utilities {
private $db;
private $msg = [];
function __construct ($db) {
$this->db = $db;
}
public function getUser ($user) {
$sql = "SELECT * FROM user_tb WHERE username = ?";
$stmt = $this->db->run($sql, [$user]);
$result = $stmt->fetch();
return $result;
}
}
退出類橫截面
include_once "utilities.php";
class Withdraw extends Utilities {
private $db;
function __construct($db) {
$this->db = $db;
}
public function withdrawer ($username, $addr, $coin, $amount) {
$sql = "SELECT * FROM wallet_tb WHERE username = ?";
$stmt = $this->db->run($sql, [$username]);
$result = $stmt->fetch();
$balance = $result["balance"];
$transact_id = $result["transact_id"];
$get = $this->getUser($username); //getUser() is from utilities class;
return $get["email"];
}
目的
$db = new Init();
$obj = new Withdraw($db);
$msg = $obj->withdrawer($user, $addr, $coin, $amount);
echo ($msg);
PS:如果我沒有從資料庫中獲取任何內容,代碼 (getUser() ) 可以正常作業
uj5u.com熱心網友回復:
您的第一個問題是,由于 $db 屬性在 Utilities 類中被宣告為“私有”,因此 Withdraw 類不能直接讀取或寫入它。您可以在子類中宣告一個具有相同名稱的單獨私有屬性,并且兩者將不相關。
如果您希望相同的屬性在兩個類中可見,您可以在 Utilities 中將其宣告為“受保護”,然后無需在 Withdraw 中再次宣告它。
您的第二個問題(正如您所發現的)是如果您在子類中撰寫建構式,PHP 不會自動呼叫父建構式。如果父類的建構式完成了你需要的一切,你可以簡單地不在子類上定義一個。如果您確實需要子類上的一個,請確保parent::__construct()使用適當的引數進行呼叫,通常在子建構式的最開始或最結束時呼叫。
最后,值得考慮的是繼承是否是這項特定作業的正確工具。繼承意味著兩個類之間存在“is-a”關系,在本例中為“Withdraw is-a Utilities”,這沒有多大意義。如果您只想讓一個類的功能在另一個類中可用,將一個類的實體傳遞給另一個類,并簡單地呼叫公共方法(稱為“依賴注入”)通常更靈活且更易于理解。例如:
class Withdraw {
private Utilities $utilities;
private DatabaseConnection $db;
function __construct(Utilities $utilities, DatabaseConnection $db) {
$this->utilities = $utilities;
$this->db = $db;
}
public function withdrawer ($username, $addr, $coin, $amount) {
$sql = "SELECT * FROM wallet_tb WHERE username = ?";
$stmt = $this->db->run($sql, [$username]);
$result = $stmt->fetch();
$balance = $result["balance"];
$transact_id = $result["transact_id"];
$get = $this->utilities->getUser($username); //getUser() is from utilities class;
return $get["email"];
}
uj5u.com熱心網友回復:
我想通了: 父母:: 做了魔法。
function __construct($db) {
$this->db = $db; // methods in the current class uses the property;
parent::__construct($db); // this runs the contruct of the other class;
}
我不知道這是否是最好的方法,但它確實有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483878.html
