慢慢長尋夜,明月高空掛php
__construct() - 在每次建立新對象時先調用此方法mysql
__destruct() - 對象的全部引用都被刪除或者當對象被顯式銷燬時執行sql
<?php /** * 清晰的認識__construct() __destruct */ class Example { public static $link; //在類實例化的時候自動加載__construct這個方法 public function __construct($localhost, $username, $password, $db) { self::$link = mysql_connect($localhost, $username, $password); if (mysql_errno()) { die('錯誤:' . mysql_error()); } mysql_set_charset('utf8'); mysql_select_db($db); } /** * 經過__construct連接好數據庫而後執行sql語句...... */ //當類須要被刪除或者銷燬這個類的時候自動加載__destruct這個方法 public function __destruct() { echo '<pre>'; var_dump(self::$link); mysql_close(self::$link); var_dump(self::$link); } } $mysql = new Example('localhost', 'root', 'root', 'test');
結果:數據庫
resource(2) of type (mysql link) resource(2) of type (Unknown)