問(wèn)題描述
很短的問(wèn)題,這是一個(gè)例子:
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);
(從頭開(kāi)始寫,可能包含語(yǔ)法錯(cuò)誤)
(written from top of the head, could contain syntax errors)
這兩個(gè)是否直接獲取到所述對(duì)象的私有屬性?我讀到它也繞過(guò)了 __construct
函數(shù),那么它也會(huì)繞過(guò)私有狀態(tài)嗎?
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?
推薦答案
非常簡(jiǎn)短的回答:是的.
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
<小時(shí)>
旁白:
這會(huì)導(dǎo)致語(yǔ)法錯(cuò)誤$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.
這篇關(guān)于PHP PDO:獲取樣式 FETCH_CLASS 和 FETCH_INTO 是否獲取到私有對(duì)象屬性中?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!