时间:2021-07-01 10:21:17 帮助过:1人阅读
- <code>class test{
- protected $pdo;
- function delete($sql){
- $this->exec($sql);
- }
- function exec($sql){
- echo $this->pdo->exec($sql);
- }
- }
- $a=new test;
- $a->delete('ver');</code>
看到一个源码是这样写的。(我省略了一部分)
为什么是$this->pdo->exec($sql); 而不是$this->exec($sql);
加上这个PDO有什么作用呢?
- <code>class test{
- protected $pdo;
- function delete($sql){
- $this->exec($sql);
- }
- function exec($sql){
- echo $this->pdo->exec($sql);
- }
- }
- $a=new test;
- $a->delete('ver');</code>
看到一个源码是这样写的。(我省略了一部分)
为什么是$this->pdo->exec($sql); 而不是$this->exec($sql);
加上这个PDO有什么作用呢?
protected $pdo;代表的就是和数据的连接。当需要执行SQL命令时,当然需要用到这个变量啊。
估计 __construct 中 有 $this->pdo = new PDO();之类的
$this->pdo 其实是指向了PDO这个类
$this 只是当前类
上面你的报错信息是说 你的 $sql 这个参数没传
因为你没有初始化pdo
- <code class="php">public function __construct($pdo)
- {
- $this->pdo = $pdo;
- }</code>