批改狀態(tài):合格
老師批語(yǔ):接口與抽象類(lèi)很像, 使用的時(shí)候, 大多數(shù)情況下, 的確是可以互換的, 所以一定要多多留意
一、抽象類(lèi)的繼承
<?php namespace _1009; abstract class Animal { public $name; public $color; public $weight; public $width; public $height; public function __construct ($name, $color, $weight, $width, $height) { $this->name = $name; $this->color = $color; $this->weight = $weight; $this->width = $width; $this->height = $height; } abstract public function eat(); abstract public function drink(); abstract public function run(); } class Cat extends Animal { public function __construct ($name='小花貓', $color='白色', $weight='10kg', $width='20公分', $height='10公分') { parent::__construct($name, $color, $weight, $width, $height); $this->name = $name; $this->color = $color; $this->weight = $weight; $this->width = $width; $this->height = $height; } public function eat () { echo 'This is eat'; } public function drink () { echo 'This is drink'; } public function run () { echo 'This is run'; } public function getInfo () { echo '我的名字是'.$this->name.', 顏色:'.$this->color.' 體重:'.$this->weight.' 寬度:'.$this->width.' 身高'.$this->height; } } $obj = new Cat(); $obj->getInfo();
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
二、接口實(shí)現(xiàn)案例
<?php interface iCurd { //添加 public function add($data); //刪除 public function del($where); //修改 public function update($data, $where); //查詢(xún) public function select(); //獲取get post 請(qǐng)求參數(shù) public function input($str); } //數(shù)據(jù)庫(kù)類(lèi) class Db implements iCurd { //數(shù)據(jù)庫(kù)連接對(duì)象 protected $pdo = null; //連接的數(shù)據(jù)表 protected $table; //請(qǐng)求類(lèi)型 protected $type; //請(qǐng)求參數(shù) protected $param; public function __construct($dsn, $username, $password, $table='staff'){ $this->pdo = new \PDO($dsn, $username, $password); $this->table = $table; } //添加 public function add($data){ //字段 $fields = ' (name, age, sex, position, mobile, hiredate) '; //值 $values = ' (:name, :age, :sex, :position, :mobile, :hiredate) '; $sql = "insert into ".$this->table.$fields.'values'.$values; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return [ 'count' => $stmt->rowCount(), 'id' => $this->pdo->lastInsertId() ]; } //刪除 public function del($where){ $sql = 'delete from '.$this->table.' where '.$where; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->rowCount(); } //修改 public function update($data, $where){ $keys = array_keys($data); $set = ''; foreach($keys as $value){ $set .= $value.'='.':'.$value.','; } $set = rtrim($set, ','); $sql = 'update '.$this->table.' set '.$set.' where '.$where; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->rowCount(); } //查詢(xún) public function select($fields='*', $where='', $limit='0, 5'){ $where = empty($where) ? '' : ' where '.$where; $limit = ' limit '.$limit; $sql = 'select '.$fields.' from '.$this->table.$where.$limit; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(\PDO::FETCHA_ASSOC); } //字符串處理 public function setStr($str){ $str_arr = explode('.' ,strtolower(trim($str))); $this->type = $str_arr[0]; $this->param = $str_arr[1]; } //獲取請(qǐng)求參數(shù) public function input($str){ $this->setStr($str); if($this->type=='get'){ return $_GET["$this->param"]; } if($this->type=='post'){ return $_POST["$this->param"]; } } } //數(shù)據(jù)庫(kù)實(shí)例 $dsn = "mysql:localhost=127.0.0.1;dbname=phpcncms"; $username = 'root'; $password = ''; $obj = new Db($dsn, $username, $password); $id = $obj->input('get.id');
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
總結(jié):
1 在繼承抽象類(lèi)時(shí),子類(lèi)中要手動(dòng)實(shí)現(xiàn)構(gòu)造方法,因?yàn)樽宇?lèi)不會(huì)自動(dòng)繼承抽象類(lèi)的構(gòu)造方法
2 接口要使用interface關(guān)鍵字,接口中方法不能有實(shí)體,即抽象方法。在實(shí)現(xiàn)接口時(shí)使用implements關(guān)鍵字,接口中的抽象方法都要被實(shí)現(xiàn)。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)