PHP PDO:获取样式FETCH_CLASS和FETCH_INTO获取到私有对象属性? [英] PHP PDO: Do the fetch styles FETCH_CLASS and FETCH_INTO fetch into private object properties?

查看:79
本文介绍了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)

这两个是否直接提取到所述对象的 private 属性中?
我读它也绕过了 __ 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?

推荐答案

很简单的答案:是的。

class Foo
{
    private $id;
    public function echoID()
    {
        echo $this->id;
    }
}
$result = $statement->fetchAll(PDO::FETCH_CLASS, "Foo");
$result[0]->echoID(); // your ID






Aside:


Aside:

这将导致语法错误 $ statement-> fetchAll(PDO :: FETCH_INTO,$ User); 。您不能使用 fetchAll 方法使用 FETCH_INTO

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获取到私有对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆