1、抽象类
<?php
//抽象类
abstract class a
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
public function af()
{
echo $this->name.'111';
}
// 这个方法是抽象方法
abstract public function aff();
}
class b extends a
{
//必须重写这个方法
public function aff()
{
echo $this->name.'222';
}
}
$b = new b('欧阳克');
echo $b->aff();
abstract class Person
{
protected $name;
protected function __construct($name)
{
$this->name =$name;
}
public function getName($name='欧阳克')
{
return $this->name;
}
abstract protected function setName($v);
}
class Stu extends Person {
public function __construct($name='欧阳克')
{
parent::__construct($name);
}
public function setName($v)
{
$this->name = $v;
}
}
$s = new Stu('诸葛亮');
//echo 'PHP中文网创始人'.$s->getName();
$s -> setName('朱老师');
echo 'php中文网前段讲师:'. $s->getName();
2、接口
<?php
//接口
//必须全部是抽象方法
interface V
{
const LXR = '欧阳克';
public function setFuel($fuel);
public function setPurpose($purpose);
}
abstract class Auro implements V{
public $fuel;
public function setFuel($fuel)
{
$this->fuel = $fuel;
}
}
class Car implements V{
public $fuel;
public $purpose;
public function __construct($purpose,$fuel)
{
$this->purpose =$purpose;
$this->fuel =$fuel;
}
public function setFuel($fuel)
{
$this->fuel = $fuel;
}
public function setPurpose($purpose)
{
$this->purpose = $purpose;
}
public function getInfo()
{
return $this->fuel.$this->purpose;
}
}
$ks = new Car('电动车','公交');
echo $ks ->getInfo('zhu');
3、上次的作业继续继续完成 封装Db类
<?php
//公共方法库
class Db
{
protected $table = null;
protected $pdo;
public function __construct($dsn,$user,$password,$table)
{
$this->pdo = new PDO($dsn,$user,$password);
$this->table = $table;
}
// 查询方法
//select * from biao WHERE 条件 order by 排序 limit 页
public function select($where='',$order='',$limilt='0,10', $filed='*')
{
//select from 必须有
$sql = 'SELECT ';
if($filed == '*'){
$sql .= '* FROM ';
} else{
$sql .= $filed.' FROM ';
}
$sql .= $this->table;
if(!$where){
$sql .= ' WHERE '.$where;
}
print_r($sql);
if($order)
{
$sql .= ' ORDER BY '.$order;
}
print_r($sql);
$stmt = $this->pdo->prepare($sql);
if($stmt->execute())
{
if($stmt->rowCount()>0){
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetchAll();
}
}else {
return false;
}
$sql = 'SELECT FROM';
}
//插入数据
public function insert($table,$data)
{
$sql = "INSERT INTO `{$table}` SET ";
//组装插入语句
if(is_array($data)){
foreach ($data as $k=>$v) {
$sql .= $k.'="'.$v.'", ';
}
}else{
return false;
}
// print_r($sql);
//去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),',').';';
//创建PDO预处理对象
// print_r($sql);
$stmt = $this->pdo->prepare($sql);
//执行新增操作
// print_r($stmt);
if($stmt->execute()){
if($stmt->rowCount()>0){
$num = $stmt->rowCount();
if($num){
$id = $this->pdo->lastInsertId();
echo "<h3>成功新增 {$num}了条记录.新增记录的主键id:{$id}</h3>";
}
}
} else {
return false;
}
// print_r($stmt);
}
// 更新操作
function update($table,$data,$where='') {
//创建SQL语句
$sql = "UPDATE `{$table}` SET ";
//组装修改语句
if(is_array($data)){
foreach ($data as $k=>$v) {
$sql .= $k.'="'.$v.'", ';
}
}
// print_r($sql);
//去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql),',');
//查询条件
if(!empty($where)){
$sql .= ' WHERE '.$where;
}
// print_r($sql);
//创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
//执行新增操作
if($stmt->execute()){
if($stmt->rowCount()>0){
$num = $stmt->rowCount();
if($num){
$id = $this->pdo->lastInsertId();
echo "<h3>成功更新 {$num}了条记录</h3>";
}
}
} else {
return false;
}
}
public function delete($table,$where){
// 预处理执行删除操作
$sql = 'DELETE FROM '.$table.' WHERE '.$where;
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount();
}
}
//实例化对象 并向 构造方法传参
$a = new Db('mysql:host=127.0.0.1;dbname=pdo','root','root','users');
////查询方法 查询多个记录
//$selects = $a->select('id=4 AND id = 11','age DESC','0,10','id,username,password');
//
//print_r($selects);
////插入数据 单条
//$insert = $a->insert('users',['username'=>'张三','password'=>sha1('123456'),'age'=>35]);
//print_r($insert);
//
//$update = $a ->update('users',['username'=>'老黄','password'=>sha1(123456),'age'=>'18'],"id=153");
//print_r($update);
// 删除记录
$where = 'id >= 150';
echo '成功删除了: ' .$a->delete('users',$where). ' 条记录';