批改狀態(tài):合格
老師批語:總結(jié)的到位
抽象類的定義和使用
php抽象類定義關(guān)鍵字 abstract
抽象方法是沒有方法體的方法,沒有方法體是指方法聲明時沒有花括號以及其中的內(nèi)容,而是聲明方法時直接在方法名后加上分號結(jié)束,聲明抽象方法要使用abstract關(guān)鍵字修飾。聲明抽象方法格式:abstract function();
抽象類中的方法不能自己去實(shí)現(xiàn),需要靠子類中去實(shí)現(xiàn)
1. 抽象類不能實(shí)例化
2. 抽象類中定義的抽象方法必須在子類中實(shí)現(xiàn)
<?php //抽象類 namespace _1009; //php抽象類定義關(guān)鍵字 abstract abstract class Person1 { //抽象類中的屬性 protected $name; //抽象方法是沒有方法體的方法,沒有方法體是指方法聲明時沒有花括號以及其中的內(nèi)容,而是聲明方法時直接在方法名后加上分號結(jié)束,聲明抽象方法要使用abstract關(guān)鍵字修飾。聲明抽象方法格式:abstract function(); // 簽名, 抽象方法 public function __construct($name = '雄') { $this->name = $name; } abstract public function getName(); abstract public function setName($value); } //抽象類中的方法不能自己去實(shí)現(xiàn),需要靠子類中去實(shí)現(xiàn) //1. 抽象類不能實(shí)例化 //2. 抽象類中定義的抽象方法必須在子類中實(shí)現(xiàn) class Stus extends Person1 { public function getName() { return $this->name; } public function setName($value) { $this->name = $value; } } $stu = new Stus('熊貓'); echo $stu->getName(); echo '<hr>'; $stu->setName('北極熊'); echo $stu->getName();
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
接口的定義和使用
定義接口類 ,關(guān)鍵字 interface
實(shí)現(xiàn)接口 ,關(guān)鍵字 implements
<?php namespace _1009; //定義接口類 ,關(guān)鍵字 interface interface Book { //設(shè)置書名 public function bookName($bookName); //設(shè)置作者 public function bookZuoZhe($bookZuoZhe); } //實(shí)現(xiàn)接口 ,關(guān)鍵字 implements class Read implements Book { public $bookName; public $bookZuoZhe; //實(shí)現(xiàn)接口里面的方法 public function bookName($bookName) { $this->bookName = $bookName; } //實(shí)現(xiàn)接口里面的方法 public function bookZuoZhe($bookZuoZhe) { $this->bookZuoZhe = $bookZuoZhe; } public function getXinxi() { return '《'.$this->bookName.'》'.'作者:'.$this->bookZuoZhe; } } //客戶端代碼調(diào)用 $obj = new Read(); $obj->bookName('三體'); $obj->bookZuoZhe('劉念慈'); echo $obj->getXinxi();
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
使用接口--實(shí)例演示數(shù)據(jù)庫的curd操作
<?php namespace _1009; //接口實(shí)戰(zhàn) interface iCurd { //增加數(shù)據(jù) public function create($data); //查詢數(shù)據(jù) public function read(); //更新數(shù)據(jù) public function update($data,$where); //刪除數(shù)據(jù) public function delete($where); } //創(chuàng)建工作類,實(shí)現(xiàn)接口方法 class Db implements iCurd { //數(shù)據(jù)庫的連接對象 protected $pdo = null; //表名 protected $table; //構(gòu)造方法連接數(shù)據(jù)庫 public function __construct($dsn, $user, $pwd, $table = 'staff') { //保存實(shí)例化后的pdo對象 $this->pdo = new \PDO($dsn, $user, $pwd); //設(shè)置表名 $this->table = $table; } //添加數(shù)據(jù) public function create($data) { // TODO: Implement create() method. // 字段列表 $fields = ' (name, age, sex,position, mobile, hiredate) '; // 值列表 $values = ' (:name, :age, :sex, :position, :mobile, :hiredate) '; //創(chuàng)建sql語句 $sql = 'INSERT INTO '.$this->table . $fields.' VALUES '.$values; //預(yù)處理語句 $stmt = $this->pdo->prepare($sql); //執(zhí)行,綁定參數(shù) $stmt->execute($data); //返回結(jié)果 return [ //返回受影響行數(shù) 'count' => $stmt->rowCount(), //返回主鍵ID 'id' => $this->pdo->lastInsertId() ]; } //查詢數(shù)據(jù) public function read($fields = '*',$where='',$limit='0,5') { //設(shè)置條件 $where = empty($where) ? '' : ' WHERE ' . $where; // 設(shè)置顯示數(shù)量 $limit = ' LIMIT ' . $limit; // 拼接sql語句 $sql = 'SELECT '.$fields . ' FROM '.$this->table.$where.$limit; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(\PDO::FETCH_ASSOC); } //更新數(shù)據(jù) public function update($data,$where) { // 字段列表 $keys = array_keys($data); $set = ''; foreach ($keys as $value) { $set .= $value . ' = :' . $value. ', '; } $set = rtrim($set, ', '); //創(chuàng)建sql語句 $sql = 'UPDATE '.$this->table . ' SET '.$set.' WHERE '.$where; //預(yù)處理語句 $stmt = $this->pdo->prepare($sql); //執(zhí)行,綁定參數(shù) $stmt->execute($data); //返回結(jié)果 return [ //返回受影響行數(shù) 'count' => $stmt->rowCount(), //返回主鍵ID 'id' => $this->pdo->lastInsertId() ]; } //刪除數(shù)據(jù) public function delete($where) { $sql = 'DELETE FROM '.$this->table.' WHERE '.$where; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->rowCount(); } } $dsn = 'mysql:host=127.0.0.1;dbname=chen'; $user = 'root'; $password = 'root'; $obj= new Db($dsn,$user,$password); //$data = [ // 'name' => '萬劍一', // 'age' => 29, // 'sex' => 1, // 'position' => '祖?zhèn)鞅瘎∧?#39;, // 'mobile' => '1389998899', // 'hiredate' => time() //]; //$res = $obj->create($data); //echo '成功的新增了: '. $res['count'].' 條記錄,新增的記錄的主鍵ID是: ' . $res['id']; //查詢 foreach($obj->read() as $value){ print_r($value); echo '<br>'; } echo '<hr>'; //修改 //$data = [ // 'name' => '張小凡', // 'position' => '悲劇男二號' //]; //print_r($obj->update($data,'staff_id = 16')); //刪除 //echo $obj->delete('staff_id = 16');
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號