<?php
header('Content-type:text/html;charset=utf-8');
error_reporting(0);
class MySql {
protected $conn = NULL;
public function __construct(){
$this->conn = mysqli_connect('localhost','root','2');
if (!$this->conn) {
$e = new Exception('漏油了',9);
throw $e;
}
}
}
try{
$mysql = new MySql();
} catch(Exception $e){
echo "捕捉到错误信息:<br/>";
echo $e->getMessage(),'<br>';
echo '错误代码:',$e->getMessage(),'<br/>';
echo '错误文件:',$e->getFile(),'<br/>';
echo '错误行:',$e->getLine(),'<br/>';
}
echo "<pre>";
var_dump($mysql);
echo "</pre>";
if ($mysql instanceof MySql) {
echo "对象创建成功,大概连接成功";
}else{
echo "对象创建失败,大概连接失败";
}
<?php
header('Content-type:text/html;charset=utf-8');
class mysql {
protected $conn = NULL;
public function __construct(){
$this->conn = mysqli_connect('localhost','root','2');
if (!$this->conn) {
$e = new Exception('漏油了',9);
throw $e;
}
}
}
$mysql = new mysql();
?>