我有一個項目(目錄)test。xampp/htdocs在這個項目中,我有 2 個 php 文件(Account.php 和 Test.php)。//---Account.php---<?phpclass Account{ protected int $id; protected string $email; protected string $pass; function __construct(string $email, string $pass, int $id = 0) { $this->id = $id; $this->email = $email; $this->pass = $pass; }}//---Test.php---<?phprequire_once("Account.php");class Test{ public function index(){ $hostname="localhost"; $database="mydb"; $username="root"; $password=""; $mysqli = new mysqli($hostname, $username, $password, $database); $result = $mysqli->query("SELECT * FROM accounts WHERE email = 'my@email.com';"); if($result) { $obj = $result->fetch_object("Account"); //ArgumentCountError if ($obj instanceof Account) { printf($obj->email); } } }}(new Test())->index();我在瀏覽器中通過:運行它http://localhost/test/test.php,我得到一個錯誤。Fatal error: Uncaught ArgumentCountError: Too few arguments to function Account::__construct(), 0 passed and at least 2 expected in C:\xampp\htdocs\test\Account.php:9 Stack trace: #0 [internal function]: Account->__construct() #1 C:\xampp\htdocs\test\Test.php(19): mysqli_result->fetch_object('Account') #2 C:\xampp\htdocs\test\Test.php(33): Test->index() #3 {main} thrown in C:\xampp\htdocs\test\Account.php on line 9如果我從中刪除參數(shù)fetch_object("Account") -> fetch_object(),則不再有錯誤并且可以工作。但我想將它與參數(shù)一起使用。為什么使用參數(shù)會產生錯誤以及如何解決?我的 PHP 版本是 7.4
具有 class_name 參數(shù)值的 fetch_object
慕運維8079593
2022-11-12 13:52:49