Correction status:qualified
Teacher's comments:关于类的继承, 他不仅是代码复用的手段, 也是一种思维模式
1、写一个抽象类并继承它, 内容自定
<?php //抽象类中的方法不能自己去实现,需要靠子类中去实现,抽象类可以看做子类实现的规范和模板。 abstract class parentclass { public $name ; public function __construct($name) { $this->name = $name; } public function get($name) { return $this->name = $name; } abstract public function set($name, $value); } class childclass extends parentclass { public function __construct($name) { parent::__construct($name); } public function set($name, $value){ return '今年新出手机是:'.$name. '价格是:'.$value; } } $obj = new childclass('默认任务'); echo $obj->get('黎明'); echo '<hr>'; echo $obj->set('Pro',2500);
点击 "运行实例" 按钮查看在线实例
2、模仿课堂案例,写一个接口实现CURD操作, 并扩展一到二个方法
<?php namespace _1009; // 接口开发实战 interface iCurd { public function create($data); public function read(); public function update($data, $where); public function delete($where); } class Db implements iCurd { protected $pdo = null; protected $table; public function __construct($dsn, $user, $password, $table='staff') { $this->pdo = new \PDO($dsn, $user, $password); $this->table = $table; } // 增加数据 public function create($data) { // SQL新增语句:INSERT INTO table_name (column1,column2,column3,...)VALUES (value1,value2,value3,...); $fields = ' (name, age, sex,position, mobile, hiredate) '; // 值列表 $values = ' (:name, :age, :sex, :position, :mobile, :hiredate) '; // 创建SQL $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 read($fields='*', $where='', $limit='0, 5') { //设置条件 SELECT * FROM table_name WHERE ; $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::FETCH_ASSOC); } // 更新数据 public function update($data, $where) { //sql:UPDATE table_name SET column1=value1,column2=value2 WHERE some_column=some_value; $keyArr = array_keys($data); $set = ''; foreach ($keyArr as $value) { $set .= $value . ' = :' . $value. ', '; } $set = rtrim($set, ', '); $sql = 'UPDATE ' . $this->table. ' SET ' . $set . ' WHERE ' . $where; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return $stmt->rowCount(); } // 删除数据 public function delete($where) { //DELETE FROM table_name WHERE some_column=some_value; $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=chenqingxuan'; $user = 'root'; $password = 'cqx07231950'; $db = new Db($dsn, $user, $password); // 读数据 //foreach ($db->read() as $item) { // print_r($item); echo '<br>'; //} //echo '<hr>'; // 新增 //$data = [ // 'name' => '郭靖', // 'age' => 29, // 'sex' => 1, // 'position' => '金刀驸马', // 'mobile' => '1389998899', // 'hiredate' => time() //]; // //$res = $db->create($data); //echo '成功的新增了: '. $res['count'].' 条记录,新增的记录的主键ID是: ' . $res['id']; //echo '<hr>'; // 更新 $data = [ 'age' => 40, 'position' => '抗金英雄' ]; $where = 'staff_id = 22'; echo '成功的更新了: ' . $db->update($data, $where) . ' 条记录'; // 删除 //$where = 'staff_id = 22'; //echo '成功的删除了: ' . $db->delete($where) . ' 条记录';
点击 "运行实例" 按钮查看在线实例