单例类
<?php
namespace _1205;
use PDO;
class demo{
}
$obj1 = new demo();
$obj2 = new demo();
var_dump($obj1 == $obj2); //true
echo '<hr>';
var_dump($obj1 === $obj2); //false
echo '<hr>';
//单例模式
//当构造方法被私有化后,它本身就只能在内部实例化(new 类名),故它就是单例模式
class Demo1{
protected function __construct()
{
}
protected static $instance;
public static function getout(){
is_null(static::$instance) ? static::$instance=new self() : '';
//也可以用下面的方法
// if(is_null(static::$instance)){
// static::$instance=new self();
// }
return static::$instance;
}
}
$obj1 = Demo1::getout();
$obj2 = Demo1::getout();
var_dump($obj1 === $obj2);
echo '<br>';
var_dump($obj1, $obj2);
echo '<hr>';
//单例模式应用场景 连接数据库
class Db{
private static $pdo = null;
public static function getDate(...$connectParam){
//这里类实例化new self(...$connectParam), 就是为了把它传参给构造方法
//is_null是为了验证第一行的$pdo是否被实例化,没有就实例化并传参给构造方法
is_null(static::$pdo) ? new self(...$connectParam) : '';
return static::$pdo;
}
protected function __construct(...$connectParam){
$dsn = $connectParam[0];
$user = $connectParam[1];
$password = $connectParam[2];
static::$pdo = new PDO($dsn,$user,$password);
}
//禁止使用克隆魔术
public function __clone()
{
}
}
$connectParam = ['mysql:host=localhost;dbname=film', 'root', 'root'];
$pdo = Db::getDate(...$connectParam);
print_r($pdo);
//查询数据库内容,也可以用query()
print_r($pdo->query('SELECT * FROM `movies`')->fetchAll(PDO::FETCH_ASSOC));
工厂模式
<?php
namespace _1205;
class demo2{
//...
}
// file1.php
$obj = new Demo2();
// file2.php
$obj = new Demo2();
// file3.php
$obj = new Demo2();
//这样调用每次都要实例化一次,很麻烦,故采用工厂模式比较方便
//工厂模式 implode() 将数组转换成字符串
class text1{
public function __construct($arg1)
{
echo '对象1=' . $arg1;
}
}
class text2{
public function __construct($arg1, $arg2)
{
echo '对象2=' . implode(', ', [$arg1, $arg2]);
}
}
class text3{
public function __construct($arg1, $arg2, $arg3)
{
echo '对象3=' . implode(', ', [$arg1, $arg2, $arg3]);
}
}
//使用工厂模式,专门创建类实例/对象
class Factory{
public static function create($classname, ...$argments){
return new $classname(...$argments);
}
}
// 类名::class 完整的类名,命名空间
Factory::create(text1::class, 100);
echo '<br>';
Factory::create(text2::class, 100,200);
echo '<br>';
Factory::create(text3::class, 100,200,300);
依赖注入
<?php
// 依赖注入:解决对象
// 工作类
class Person{
//要依赖的外部对象
private $car = null;
public function __construct()
{
//在构造方法中将外部对象全部实例化
//注入点在构造方法中
$this->car = new Car();
}
public function work(){
return $this->car->drive();
}
}
// 依赖的外部类
class car{
public function drive(){
return '开车上班';
}
}
// 客服端实例化
$person = new Person();
echo $person->work();
echo '<hr>';
/*******************************************/
// 最傻瓜的依赖输入方式二
class Person1{
private static $car = null;
public function __construct(Car1 $car1)
{
static::$car = $car1;
}
public function work(){
return static::$car->drive();
}
}
class Car1{
public function drive(){
return '开皮卡去上班';
}
}
$car1 = new Car1();
$person1 = new Person1($car1);
echo $person1->work();
自动连接 autoload.php
<?php
spl_autoload_register(function ($className){
$path = str_replace('\\', '/', $className);
require dirname(__DIR__) . DIRECTORY_SEPARATOR . $path . '.php';
});
工厂模式小案例
<?php
namespace demo;
use demo\Vehicle\car;
use demo\Vehicle\bus;
use demo\Vehicle\ship;
require __DIR__ . '/autoload.php';
// 工厂类, 专用于创建类实例
class Factroy{
protected static $intance = null;
public static function getDrive($variable){
switch ($variable) {
case 'car':
static::$intance = new car();
break;
case 'bus':
static::$intance = new bus();
break;
case 'ship':
static::$intance = new ship();
break;
}
// 返回当前具体的交通工具
return static::$intance;
}
}
class drive{
// 交通工具
protected static $vehicle;
// 构造方法 调用工厂,传参
public function __construct($variable)
{
static::$vehicle = Factroy::getDrive($variable);
}
// 调用外部一个依赖对象
public function trave(){
return static::$vehicle->drive() . ' 美好生活';
}
}
//客户端输出
echo (new drive('ship'))->trave() . '<br>';
echo (new drive('car'))->trave() . '<br>';
echo (new drive('bus'))->trave() . '<br>';
//#外部类################
//#外部类################
//#外部类################
<?php
namespace demo\Vehicle;
class bus{
public function drive(){
return '坐公交车';
}
}
===============================
<?php
namespace demo\Vehicle;
class car{
public function drive(){
return '开汽车';
}
}
===============================
<?php
namespace demo\Vehicle;
class ship{
public function drive(){
return '坐轮船';
}
}
接口类 代替 工厂模式
<?php
namespace demo;
use demo\Vehicle\train;
use demo\Vehicle\plane;
use demo\Vehicle\iVehicle;
require __DIR__ . '/autoload.php';
class travee{
// 交通工具
private static $vehicle;
// 构造方法
public function __construct(iVehicle $vehicle)
{
static::$vehicle = $vehicle;
}
// 调用外部一个依赖对象
public function go()
{
return static::$vehicle->drive() . ' : ======= 去旅行';
}
}
//客户端调用
echo (new travee(new plane()))->go(),'<br>';
echo (new travee(new train()))->go();
//#####################
//#####################
//#####################
<?php
namespace demo\Vehicle;
//接口
interface iVehicle{
public function drive();
}
===========================
<?php
namespace demo\Vehicle;
class plane implements iVehicle{
public function drive(){
return '坐飞机';
}
}
===========================
<?php
namespace demo\Vehicle;
class train implements iVehicle{
public function drive(){
return '坐火车';
}
}
总结
本节课相对简单,今天好好做了一次,基本掌握了