PHP PDO: Do the fetch styles FETCH_CLASS and FETCH_INTO fetch into private object properties?(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 是否获取到私有对象属性中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP PDO:获取样式 FETCH_CLASS 和 FETCH_INTO 是否获取到私有对象属性中?


基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01