PDO FETCH_CLASS 和命名空间问题 [英] PDO FETCH_CLASS and Namespacing issue

查看:53
本文介绍了PDO FETCH_CLASS 和命名空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在对象上使用 PDO::FETCH_CLASS.我正在使用命名空间并输入:

I am trying to use PDO::FETCH_CLASS on an object. I am using namespacing and just entering:

$result = $query->fetchAll(\PDO::FETCH_CLASS, 'Product');

$result = $query->fetchAll(\PDO::FETCH_CLASS, '\Product');

导致 PHP 在应用程序的根目录中查找 Product.php.

results in PHP looking for Product.php in the root of the app.

我可以使用以下方法成功实例化一个新产品:

I can successfully instantiate a new Product though using:

 $product = new Product();

所以我知道我的姓名间距有效.

So I know my name spacing is working.

这不可能吗?还是我需要先实例化一个产品,然后再从查询中填充它?

Is this not possible? Or do I need to instantiate a Product first then populate it after from the query?

推荐答案

我怀疑 PDO 不会查找别名类名称或解析当前命名空间.所以你必须明确地传递它:

I'd suspect PDO does not look up aliased class names or resolve the current namespace. So you have to pass it explicitely:

= $query->fetchAll(\PDO::FETCH_CLASS,  __NAMESPACE__ . '\\Product');

精确性挑剔:请注意,虽然单个反斜杠确实有效,但在单引号中它旨在转义文字单引号本身.

Exactness nitpick: Note that while a single backslash does work, in single quotes it is intended to escape literal single quotes and itself.

这篇关于PDO FETCH_CLASS 和命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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