Maison >développement back-end >tutoriel php >在构造方法中使用静态属性保存的PDO资源句柄在其他方法中无法调用?
<?phpclass DB{ protected $link = '127.0.0.1'; protected $dbname = 'think'; static public $DB; private function __construct(){ try{ self::$DB = new PDO("mysql:host={$this->link};dbname={$this->dbname}",'root','root'); }catch (PDOException $e){ die("连接出错:".$e->getMessage()); } $sql = ' SELECT * FROM user WHERE id=? and username=? and email=? '; $stmt = self::$DB->prepare($sql); $stmt->execute([0=>'13',1=>'12',2=>'12']); echo '<pre class="brush:php;toolbar:false">'; print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); echo $stmt->rowCount(); } //静态方法,单例统一访问入口 static public function getInstance() { if (is_null ( self::$DB ) || isset ( self::$DB )) { self::$DB = new self (); } return self::$DB; } public function Test(){ $sql = ' SELECT * FROM user WHERE id=? and username=? and email=? '; $stmt = self::$DB->prepare($sql); $stmt->execute([0=>'13',1=>'12',2=>'12']); echo '<pre class="brush:php;toolbar:false">'; print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); echo $stmt->rowCount(); }}$db = DB::getInstance();$db->Test();
$DB->prepare没有prepare方法,说明$DB可能没有被实例化,在实例化之后打印DB类里吗的$DB变量看看
在构造函数中,self::$DB 是 PDO 对象,所以有prepare 方法
在 getInstance 方法中,你又 self::$DB = new self (); 把 self::$DB 赋值为 DB 对象,所以就没有了 prepare 方法
你至少应写作
class DB{ protected $link = '127.0.0.1'; protected $dbname = 'think'; static public $DB; static public $_DB; private function __construct(){ try{ self::$_DB = new PDO("mysql:host={$this->link};dbname={$this->dbname}",'root','root'); }catch (PDOException $e){ die("连接出错:".$e->getMessage()); } } //静态方法,单例统一访问入口 static public function getInstance() { if (is_null ( self::$DB ) || isset ( self::$DB )) { self::$DB = new self (); } return self::$DB; } public function Test(){ $sql = ' SELECT * FROM user WHERE id=? and username=? and email=? '; $stmt = self::$_DB->prepare($sql); $stmt->execute([0=>'13',1=>'12',2=>'12']); echo '<pre class="brush:php;toolbar:false">'; print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); echo $stmt->rowCount(); }}$db = DB::getInstance();$db->Test();PDO 本身已经封装的很好了,如确需要进一步封装以简化调用代码
class DB extends PDO { private static $_Instance; function __construct() { $options = array( PDO::MYSQL_ATTR_INIT_COMMAND => "set names gbk", PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ); parent::__construct('mysql:dbname=test', 'root', '', $options); } //执行各种 sql 指令,并可通过参数 $param 进行扩展 function query($sql, $param=null) { $res = []; try { $rs = parent::query($sql); do { if($t = $rs->fetchall()) $res[] = $t; }while($rs->nextRowset()); return $res; } catch (PDOException $e) { die( "Error!: " . $e->getMessage() . "\n" ); // die(); } } //查询并返回单条记录 static function fetch($sql) { if(! self::$_Instance) self::$_Instance = new self; return self::$_Instance->query($sql)[0][0];//->fetch(); } //查询并以数组方式返回多条记录 static function fetchall($sql) { if(! self::$_Instance) self::$_Instance = new self; $res = self::$_Instance->query($sql);//->fetchall(); if(count($res) == 1) return current($res); }}这样你就有机会这样使用了
if (!self::$DB instanceof self) { self::$DB = new self (); } return self::$DB;
多谢@xuzuning