简体   繁体   English

PDO FETCH_CLASS和命名空间问题

[英]PDO FETCH_CLASS and Namespacing issue

I am trying to use PDO::FETCH_CLASS on an object. 我试图在对象上使用PDO :: FETCH_CLASS。 I am using namespacing and just entering: 我正在使用命名空间,只需输入:

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

or 要么

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

results in PHP looking for Product.php in the root of the app. 导致PHP在应用程序的根目录中查找Product.php

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? 或者我是否需要首先实例化产品然后从查询后填充它?

I'd suspect PDO does not look up aliased class names or resolve the current namespace. 我怀疑PDO不会查找别名的类名或解析当前的命名空间。 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. Exactness nitpick:请注意,虽然单个反斜杠确实有效,但在单引号中它旨在逃避文字单引号及其本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM