PDO FETCH

编程入门 行业动态 更新时间:2024-10-27 19:22:02
本文介绍了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

本文发布于:2023-10-26 20:36:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PDO   FETCH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!