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.
相关文章