简体   繁体   English

使用名称空间的pdo库

[英]with pdo library using namespace

Everything works fine as long as I have not added namespace. 只要我没有添加名称空间,一切都可以正常工作。 Befor PDO i add /, but stil PDO object is empty, why? Befor PDO我添加了/,但是stil PDO对象为空,为什么? Whats wrong with my code? 我的代码有什么问题?

Full sampe: 全样品:

<?php
namespace ProjectM;

class classPDO extends DbCore
{
    public function get()
    {               
        $this->dbLink = new \PDO('mysql:host=localhost;dbname=test_db', 'root', 'pass1312');

        print_r($this->dbLink); // - always empty

        $res = $this->dbLink('SELECT * FROM product')->fetchAll(PDO::FETCH_ASSOC);  

        print_r($res); // - always empty
    }

Error: Fatal error: Call to undefined method Core\\classPDO::dbLink() 错误:致命错误:调用未定义的方法Core \\ classPDO :: dbLink()

Why "$this->dbLink" is empty? 为什么“ $ this-> dbLink”为空? Thanks 谢谢

Your new \\PDO instantiation is correct. new \\PDO实例化是正确的。 Your property ->dbLink gets created alright. 您的属性->dbLink会被创建。 But the PDO instance does not implement __call . 但是PDO实例不实现__call Thus this won't work: 因此,这将不起作用:

$res = $this->dbLink('SELECT ...')

You still need to follow the API of the PDO class. 您仍然需要遵循PDO类的API。
In your case, use the ->query method after referencing your PDO instance property: 对于您的情况,请在引用您的PDO实例属性后使用->query方法:

$res = $this->dbLink->query('SELECT ...')

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

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