<?php
// 抽象类abstract声明抽象类 不能被实例化
// 抽象类经常用于描述一些事物所具有的特征和行为,但是没有具体的实现
// 抽象类:设计与实现的分离 抽象类可以为子类提供一些公共的接口以作为子类重写的模板来使用
abstract class Person
{
protected $name;
protected $country;
public function __construct($name='',$country='喵星')
{
$this->name = $name;
$this->country = $country;
}
// 抽象方法
abstract function say();
abstract function eat();
// 普通方法
public function run()
{
echo '两条腿走路<br>';
}
}
// abstract class Chinese extends Person{
// // 子类按照自己的需求去实现抽象方法,如果抽象方法未全部实现 那么该类依然是一个抽象类
// public function say()
// {
// echo "$this->name 是{$this->country}人,他说的是{$this->country}话。<br>";
// }
// }
class Chinese extends Person{
// 子类按照自己的需求去实现抽象方法,如果抽象方法未全部实现 那么该类依然是一个抽象类
public function say()
{
echo "$this->name 是{$this->country}人,他说的是{$this->country}话。<br>";
}
public function eat()
{
echo "{$this->name} 用筷子吃饭。<br>";
}
}
$Chinese = new Chinese("柯基大人",'中国');
$Chinese->say();
$Chinese->run();
$Chinese->eat();
echo '<hr>';
class American extends Person{
public function say()
{
echo "$this->name 是{$this->country}人,他说的是{$this->country}话。<br>";
}
public function eat()
{
echo "{$this->name} 用刀叉吃饭。<br>";
}
}
$a = new American ('MIKE','美国');
$a->say();
$a->run();
$a->eat();
?>
<?php
// 接口 interface 接口是定义 类的实现 trait结构 解决oop高耦合
interface Idemo
{
// 接口中只允许声明两类成员 1.接口常量 2.公共的抽象方法
const APP_NAME = 'hidemi小铺';
public static function getInfo(...$info);
public static function cal($one,$two);
}
interface Idemo2
{
}
// 接口的实现 implements
class Work implements Idemo
{
static function getInfo(...$info)
{
return print_r($info,true);
}
static function cal($one,$two)
{
return $one * $two;
}
}
ECHO Work::APP_NAME;
ECHO Idemo::APP_NAME;
echo work::getInfo('古力娜扎','28',172,'新疆');
echo work::cal(58,69);
// 先继承再实现
// 工作类exterface2 抽象类(普通类)implements interface1,interface2,...
echo'<hr>';
// oop封装 继承 多态
// 多态实现了动态绑定的功能
// PHP中多态指的是方法的重写
// 为计算机(主程序)扩展功能 接口是定义
interface USB
{
function run();
}
// 为计算机扩展功能 扩展了一个额USB 键盘 实现USE接口
class Ukey implements USB
{
function run()
{
return '运行USB键盘设备<br>';
}
}
class Umemo implements USB
{
function run()
{
return '运行USB储存盘设备<br>';
}
}
class Umouse implements USB
{
function run()
{
return '运行USB鼠标设备<br>';
}
}
// 主程序计算机
class Computer
// 计算机类中的一个方法 要可以应用任意一种USB设备
{function useUSB($usb)//参数是一个对象
{
return $usb->run();
}
}
$c = new Computer;
echo $c->useUSB(new Umouse());//为计算机主程序插入use鼠标并运行
echo $c->useUSB(new Ukey());//为计算机主程序插入use鼠标并运行
echo $c->useUSB(new Umemo());//为计算机主程序插入use鼠标并运行
?>
<?php
// 后期静态绑定 static
class Test
{
public function connect():string
{
return "父类Test中的方法".__METHOD__.'<br>';
}
public function show():string
{
return $this->connrct();
}
}
class Pro extends Test{
public function connect():string
{
return "子类Test中的方法".__METHOD__.'<br>';
}
}
// 在传统继承上下文中 $this可以动态的和调用对象进行绑定
echo (new Test)->connect();
echo (new pro)->connect();
echo (new Test)->show();//父类Test中的方法Test::connect
echo (new pro)->show();//子类Test中的方法Test::connect
class Father
{
protected static $_instance = null;
static public function getInstance()
{
//单例模式 保证实例化只发生一次
// self始终和当前类进行绑定
// 在静态的继承上下文中,self不能动态的识别它的调用者
if(self::$_instance === null)
{
self::$_instance === new self();//返回的实例是变不变的 无论谁去调用 返回的是同一个类的实例
}
return self::$_instance;
}
}
class Son1 extends Father
{
protected static $_instance = null;
}
class Son2 extends Father
{
protected static $_instance = null;
}
var_dump(Father::getInstance());
var_dump(Son1::getInstance());
var_dump(Son2::getInstance());
// 使用self实例化得到的结果依然是Father类的同一个对象
?>
<?php
// 后期静态绑定 static
class Test
{
public function connect():string
{
return "父类Test中的方法".__METHOD__.'<br>';
}
public function show():string
{
return $this->connrct();
}
}
class Pro extends Test{
public function connect():string
{
return "子类Test中的方法".__METHOD__.'<br>';
}
}
// 在传统继承上下文中 $this可以动态的和调用对象进行绑定
echo (new Test)->connect();
echo (new pro)->connect();
echo (new Test)->show();//父类Test中的方法Test::connect
echo (new pro)->show();//子类Test中的方法Test::connect
class Father
{
protected static $_instance = null;
static public function getInstance()
{
//单例模式 保证实例化只发生一次
// 后期静态绑定开始
if(static::$_instance === null)
{
static::$_instance === new static;//new static 生成的实例 是由调用对象决定的
}
return static::$_instance;
}
}
class Son1 extends Father
{
protected static $_instance = null;
}
class Son2 extends Father
{
protected static $_instance = null;
}
var_dump(Father::getInstance());
var_dump(Son1::getInstance());
var_dump(Son2::getInstance());
// 使用self实例化得到的结果依然是Father类的同一个对象
?>
<?php
// 单例模式连接数据库
interface iDbBase
{
// 数据库操作 curd
static function insert($db,$data);
static function select($db,$where=[]);
static function delete($db,$where=[]);
static function update($db,$data,$where=[]);
static function insert($db,$username,$password);
}
abstract class aDb implements iDbBase
{
// 创建类的唯一实例 pdo对象
private static $_instance;
// private私有的 阻止此类在外部进行实例化
private function __construct()
{
}
// private私有的 阻止此类在外部进行克隆
private function __clone
{
}
static function doConnect($db,$username,$password)
{
// 得到pdo连接对象 存储在 $_instance
if(is_null(self::$_instance))
{
self::$_instance = new PDO($dsn,$username,$password);
}
return self::$_instance;
}
}
// 工作类
class Dd extends aDd{
// 数据库操作 curd
ststic function insert($db,$data){
}
static function select($db,$where=['gender'=>1,'id'=>20201]){
// select filed.. form tableName where gender=1 and id>1
// select filed.. form tableName where gender=1
$str = '';
foreach($where as $k=>$v)
{
if(is_array($where))
{
if(count($where) > 1)
{
$str .=$k . '=' . $v . 'and';
}else{
$str .=$k . '=' .$v;
}
}
}
if(count($where) > 1)
{
// 去掉where中的最后一个and
$str = substr($str,0,strlen($str)-4);
// echo $str;
}
// echo $str;
return $db->query("SELECT `uname`, 'tel' FROM `iuser` WHERE $str ")->fetchAll(PDO::FETCH_ASSOC);
}
static function delete($db,$where=[]){
}
static function update($db,$data,$where=[]){
}
}
// 客户端代码
$dsn = 'mysql:host=39.105.79.62;dbname=news';
$db = Db::doConnect($dsn,'miujue','zhoujielun521');
var_dump($db);
print_r(Db::select($db));
?>