PDO FETCH_CLASS and Namespacing issue

匆匆过客 提交于 2020-01-30 05:12:34

问题


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');

or

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/7914914/pdo-fetch-class-and-namespacing-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!