PDO PHP 获取类

PDO PHP Fetch Class(PDO PHP 获取类)
本文介绍了PDO PHP 获取类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在 php 中学习 pdo,以便使数据库访问更容易和更高效.我读过的一个关于 fetch _class 的解释是你的对象的属性是在调用构造函数之前设置的.这是什么意思?任何方向都非常感谢.

I am learning pdo in php , so as to make database access easier and more efficient .One explanation i have read for fetch _class is that The properties of your object are set BEFORE the constructor is called.What does this mean? Any direction is greatly appreciated.

推荐答案

这意味着在使用 PDO 将结果返回到自定义对象时,需要设置查询结果键对应的成员变量.

This means that when using PDO to return a result into a custom object, you are required to set out the member variables which correspond to the query result keys.

例如:

class User
{
    //Predefine Here
    public $id;
    public $username;
    public $password;
    public $email;
    public $hash;

    public function profileLink()
    {
         return sprintf('<a href="/profile/%s">%s</a>',$this->id,$this->username);
    }
}

$result = $sth->fetchAll(PDO::FETCH_CLASS, "User");
foreach($result as $user)
{
    echo $user->profileLink();
}

通过这种方式,PDO 可以将变量设置到其内部范围之外的对象.

This way PDO can set the variables to the object outside of its internal scope.

如果你的用户类是这样的:

if you user class was like so:

class User
{
}

然后 PDO 将无法从范围之外设置值,因为没有定义属性.

then PDO Would not be able to set the values from outside the scope, as there are no properties defined.

这篇关于PDO PHP 获取类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Why can#39;t I update data in an array with foreach loop?(为什么我不能用 foreach 循环更新数组中的数据?)
Foreach for arrays inside of an array(Foreach 用于数组内的数组)
PHP array get next key/value in foreach()(PHP 数组在 foreach() 中获取下一个键/值)
Using preg_match on a multidimensional array to return key values arrays(在多维数组上使用 preg_match 返回键值数组)
php foreach as key, every two number as a group(php foreach 为key,每两个数字为一组)
Treat a PHP class that implements Iterator as an array(将实现 Iterator 的 PHP 类视为数组)