本文介紹了PHP PDO:獲取樣式 FETCH_CLASS 和 FETCH_INTO 是否獲取到私有對象屬性中?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
很短的問題,這是一個例子:
Pretty short question, here is an example:
$prepared = $this->pdo->prepare("SELECT * FROM Users WHERE ID = :ID");
$statement = $prepared->execute(array(":ID" => $User_ID))
$result = $statement->fetchAll(PDO::FETCH_CLASS, "User");
//OR
$User = new User();
$result = $statement->fetch(PDO::FETCH_INTO, $User);
(從頭開始寫,可能包含語法錯誤)
(written from top of the head, could contain syntax errors)
這兩個是否直接獲取到所述對象的私有屬性?我讀到它也繞過了 __construct
函數,那么它也會繞過私有狀態嗎?
Do those two directly fetch into the private properties of said objects?
I read it also circumvents the __construct
function, so will it circumvent private status too?
推薦答案
非常簡短的回答:是的.
Very short answer: Yes it will.
class Foo
{
private $id;
public function echoID()
{
echo $this->id;
}
}
$result = $statement->fetchAll(PDO::FETCH_CLASS, "Foo");
$result[0]->echoID(); // your ID
<小時>
旁白:
這會導致語法錯誤$statement->fetchAll(PDO::FETCH_INTO, $User);
.您不能將 FETCH_INTO
與 fetchAll
方法一起使用.
This will cause syntax errors $statement->fetchAll(PDO::FETCH_INTO, $User);
. You can't use FETCH_INTO
with the fetchAll
method.
這篇關于PHP PDO:獲取樣式 FETCH_CLASS 和 FETCH_INTO 是否獲取到私有對象屬性中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!