Home >Backend Development >PHP Tutorial >Revisiting PHP
oop mode
There are two points here
One is abstract class and interface, they are different. Abstract classes can store methods with function bodies, but interfaces cannot.
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 handling
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; } } ?>