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;
}
}
?>