這兩天開始把PHP重溫下
1.oop模式
這裏有兩個點
有個是抽象類和接口,他們區別大於。抽象類可以存有函數體的方法,而接口不可以。
abstract class Top
{
public function getOne();
public function getTwo();
public function getThree()
{
return 300;
}
}
class Top_extend extends Top
{
function getOne()
{
return 100;
}
}
//接口
class interface TopInterface
{
public function getData();
}
class top_interface implements TopInterface
{
}
//$t = new Top(); //抽象類不能被直接實例化
$t = new Top_extend(); //可以通過實例子類
2.異常處理
exception.php 異常基類
//異常基類
class LogException extends Exception
{
var $logfile_dir = 'exception.log';
public function __construct($msg=null,$code=0,$file='')
{
if($file == '')
{
$file = $logfile_dir;
}
$this->saveLog($file);
parent::__construct($msg,$code);
}
//記錄日誌
protected function saveLog($file)
{
file_put_contents($file,$this->__toString(),FILE_APPEND);
}
}
<?php
//數據庫錯誤類
include_once('LogException.php');
class DataBaseException extends LogException
{
protected $databaseErrorMessage;
public function __construct($msg='',$code = 0)
{
$this->databaseErrorMessage = $msg;
parent::__construct($msg,$code);
}
public function getMsg()
{
return $this->databaseErrorMessage;
}
}
?>