第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具有 class_name 參數(shù)值的 fetch_object

具有 class_name 參數(shù)值的 fetch_object

PHP
慕運維8079593 2022-11-12 13:52:49
我有一個項目(目錄)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
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

正如手冊所說:

請注意,mysqli_fetch_object()在調用對象構造函數(shù)之前設置對象的屬性。

但是從同一本手冊中您可以看到還有第三個參數(shù)

要傳遞給 class_name 對象的構造函數(shù)的可選參數(shù)數(shù)組。

所以,你應該這樣做:

$obj = $result->fetch_object("Account", ['email', 'pass', 'id']);

如果這沒有幫助,您應該將所有構造函數(shù)參數(shù)設為可選:

function __construct(string $email = '', string $pass = '', int $id = 0)

因為fetch_object已經設置了這些道具。這也意味著您應該檢查這些參數(shù)是否為空。

或者不使用fetch_object其他東西,只顯式調用構造函數(shù),但這顯然是您想要使用的最后一個解決方案。


查看完整回答
反對 回復 2022-11-12
  • 1 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號