Correction status:qualified
Teacher's comments:接口中的public关键定, 还是要写的, 建议不要省略掉
1、写个抽象类并继承它
<?php abstract class Car { abstract protected function price($price); abstract protected function brand($brand); abstract protected function type($type); } class DZCar { protected function price($price){ return '汽车价格:'.$price; } protected function brand($brand){ return '汽车***:'.$brand; } protected function type($type){ return '汽车类型:'.$type; } public function get($brand,$price,$type){ return $this->brand($brand) . '<br>' .$this->price($price) . '<br>' . $this->type($type); } } echo (new DZCar())->get('大众','20W','汽油');
点击 "运行实例" 按钮查看在线实例
2、通过接口实现CURD操作,并扩展一到二个方法
<?php interface Db { function select($field='*',$where='',$limit='0,5'); function update($data,$where=[]); function add($data); function del($where = []); function delBatch($where = []); } class Query implements Db { protected $pdo = null; protected $table; public function __construct($dsn,$use,$pass,$table = 'demo') { $this->pdo = new \PDO($dsn,$use,$pass); $this->table = $table; } function select($field='*',$where='',$limit='0,5'){ $where = empty($where) ? '' : ' where ' . $where; $limit = ' limit '. $limit; $sql = 'select '. $field .' from ' . $this->table . $where . $limit; $smtp = $this->pdo->prepare($sql); $smtp->execute(); return $smtp->fetchAll(\PDO::FETCH_ASSOC); } function update($data,$where = []) { if(count($where) == 0){ return '条件不能为空'; } $mkey = array_merge($data,$where); $key = array_keys($data); $wkey = array_keys($where); $value = ''; foreach($key as $val){ $value .= $val .'=:' . $val . ','; } $value = rtrim($value,','); $where= ''; foreach($wkey as $w){ $where .= ' and ' . $w .'=:' . $w ; } $where = ltrim($where,' and '); $sql = 'update ' . $this->table . ' set ' . $value . ' where ' . $where; // echo $sql; $smtp = $this->pdo->prepare($sql); $smtp->execute($mkey); return $smtp->rowCount(); } function add($data) { $key = "(name,age,addr)"; $value = "(:name,:age,:addr)"; $sql = 'insert into ' . $this->table . $key . 'values' . $value; // echo $sql; $smtp = $this->pdo->prepare($sql); $smtp->execute($data); return $data = [ "count" => $smtp->rowCount(), "id" => $this->pdo->lastInsertId() ]; } function del($where = []) { if(count($where) == 0){ return '条件不能为空'; } $wkey = array_keys($where); $wval = ''; foreach($wkey as $w){ $wval .= ' and ' . $w . '=:' . $w; } $wval = ltrim($wval,' and '); $sql = 'delete from ' . $this->table . ' where ' . $wval; // echo $sql; $smtp = $this->pdo->prepare($sql); $smtp->execute($where); return $smtp->rowCount(); } function delBatch($where = []){ if(count($where) == 0){ return '条件不能为空'; } $wval = ''; $newWhere = []; foreach($where as $key=>$val){ $wval .= '?,'; $newWhere[$key+1] = $val; } $wval = trim($wval,','); $sql = 'delete from ' . $this->table . ' where id in (' . $wval . ')'; // echo $sql; $smtp = $this->pdo->prepare($sql); foreach($newWhere as $key=>$val){ $smtp->bindValue($key,$val); } $smtp->execute(); return $smtp->rowCount(); } } $dsn = 'mysql:host=127.0.0.1;dbname=test'; $use = 'root'; $pass = '000000'; $obj = new Query($dsn,$use,$pass,'testInfo'); //查询 echo '<pre>'; print_r($obj->select()); echo '<hr>'; //更新 $data = [ "name" => '小红', "addr" => '北京' ]; $where = [ 'id' => '3', 'age' => '18' ]; print_r($obj->update($data,$where)); echo '<hr>'; //删除 $where = [ 'id' => '6', // 'age' => '18' ]; print_r($obj->del($where)); echo '<hr>'; //添加 $data = [ "name" => '1', "age" => '22', "addr" => '33' ]; print_r($obj->add($data)); echo '<hr>'; //批量删除 $where = [7,8,9,10]; print_r($obj->delBatch($where));
点击 "运行实例" 按钮查看在线实例
总结:
1、抽象类内部方法可以抽象也可以是普通方法。抽象方法没有实体,需要子类去继承实现,不能是私有的,不能被实例化
2、接口类内部方法都为接口类型,必须是公共的,子类必须继承实现,可以实现多继承