$this->pdo->exec($sql);这是什么意思

class test{
      protected $pdo;
    function delete($sql){
        $this->exec($sql);
    }
    function exec($sql){
        echo $this->pdo->exec($sql);
    }
}
$a=new test;
$a->delete('ver');

看到一个源码是这样写的。(我省略了一部分)

为什么是$this->pdo->exec($sql); 而不是$this->exec($sql);
加上这个PDO有什么作用呢?

阅读 4.1k
3 个回答

protected $pdo;代表的就是和数据的连接。当需要执行SQL命令时,当然需要用到这个变量啊。

估计 __construct 中 有 $this->pdo = new PDO();之类的
$this->pdo 其实是指向了PDO这个类
$this 只是当前类
上面你的报错信息是说 你的 $sql 这个参数没传

因为你没有初始化pdo

public function __construct($pdo)
{
    $this->pdo = $pdo;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题