寫一個(gè)抽象類并繼承它, 內(nèi)容自定
模仿課堂案例,寫一個(gè)接口實(shí)現(xiàn)CURD操作, 并擴(kuò)展一到二個(gè)方法
課堂筆記:
類的自動(dòng)加載技術(shù),就是通過php中的一個(gè)方法spl_autoload_register()
spl_autoload_register(function ($className){ $path = str_replace('\\',DIRECTORY_SEPARATOR,$className); $path = __DIR__.'/'.$path.'.php'; if(file_exists($path)) include $path; }); echo \inc\Test1::get(); echo '<br>'; echo \inc\Test2::get(); echo '<br>'; echo \inc\Test3::get(); echo '<br>'; echo \inc\Test4::get(); echo '<br>';
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
寫一個(gè)抽象類并繼承它, 內(nèi)容自定
抽象類和方法就是在前面加上關(guān)鍵字abstract,抽象方法就是不用具體實(shí)現(xiàn)的方法,只有一個(gè)類中有一個(gè)抽象方法,那么該類就需要改成抽象類。抽象方法在子類中必須要實(shí)現(xiàn)。父類的構(gòu)造方法在子類中不會(huì)被繼承,所以要重新寫
<?php abstract class Person2{ protected $name; public function __construct($name = 'peter zhu') { $this->name = $name; } public function getName(){ return $this->name; } abstract public function setName($value); } class Person3 extends Person2{ public function __construct($name) { parent::__construct($name); } public function setName($value) { $this->name = $value; } } $person = new Person3('豬哥'); echo 'php中文網(wǎng)的創(chuàng)始人是'.$person->getName(); echo '<br>'; $person->setName('朱老師'); echo 'php中文網(wǎng)的講師是'.$person->getName();
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
模仿課堂案例,寫一個(gè)接口實(shí)現(xiàn)CURD操作, 并擴(kuò)展一到二個(gè)方法
類是對象的模版,接口就是類的模版,接口是用來定義里面的方法和標(biāo)準(zhǔn),讓類去具體實(shí)現(xiàn)他。用implements去實(shí)現(xiàn)他。
<?php namespace _1009; //接口開發(fā)實(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)建一個(gè)db類,實(shí)現(xiàn)iCurd接口,完成基本的數(shù)據(jù)庫操作 class Db implements iCurd{ //數(shù)據(jù)庫連接對象 protected $pdo = null; //數(shù)據(jù)表 protected $table; //構(gòu)造方法,實(shí)例化的時(shí)候連接數(shù)據(jù)庫,并設(shè)置默認(rèn)的數(shù)據(jù)表名稱 public function __construct($dsn,$user,$password,$table ='staff') { $this->pdo = new \PDO($dsn,$user,$password); $this->table = $table; } //增加數(shù)據(jù) public function create($data){ $fields = ' (name,age,sex,position,mobile,hiredate) '; $values = ' (:name,:age,:sex,:position,:mobile,:hiredate)'; $sql = 'insert into '.$this->table.$fields.' values '.$values; // die($sql); $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return [ 'count'=>$stmt->rowCount(), 'id'=>$this->pdo->lastInsertId() ]; } //讀取數(shù)據(jù) public function read($fields='*',$where='',$limit='6'){ //設(shè)置條件 $where = empty($where)?'' : 'where' .$where; //顯示數(shù)量 $limit = ' limit '.$limit; $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){ $keyArr = array_keys($data); $set = ''; foreach ($keyArr as $value){ $set.= $value . ' =:'.$value.', '; } $set= rtrim($set,', '); $sql = 'update '.$this->table. ' set '.$set.' where '.$where; echo $sql; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return $stmt->rowCount(); } //刪除數(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=huangsijie'; $user ='root'; $password = 'root'; $db = new Db($dsn,$user,$password); foreach ($db->read()as $item ){ print_r($item); echo '<br>'; } //新增數(shù)據(jù) //$data = [ // 'name'=>'郭大俠', // 'age'=>29, // 'sex'=>1, // 'position'=>'金刀駙馬', // 'mobile'=>'1888888888', // 'hiredate'=>123123 //]; // //$res = $db->create($data); //echo '成功的新增了'.$res['count'].'條記錄,新增的主鍵ID是'.$res['id']; //echo '<br>'; //echo '<br>'; //更新數(shù)據(jù) // //$data=[ // 'age'=>40, // 'position'=>'抗金英雄' //]; //$where = 'staff_id = 6'; //echo '成功更新了'.$db->update($data,$where).'條記錄'; //刪除數(shù)據(jù) $where = 'staff_id = 6'; echo '成功刪除了'.$db->delete($where).'條記錄';
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
主要是更新數(shù)據(jù)那里,需要使用array_keys($data)這個(gè)函數(shù)去獲取數(shù)組的鍵值,還有就是條件右邊的逗號用rtrim函數(shù)去掉。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號