`<?php
namespace gzg_05;
// PDO打开全局变量 如不打开 调用时需要 加 \PDO
use PDO;
// 单例模式
// 空类
class Temp{
//…
}
//实例化 两个类
$obj1 = new Temp();
$obj2 = new Temp();
var_dump($obj1,$obj2);
echo ‘<br>‘;
var_dump($obj1 === $obj2);
echo ‘<hr>‘;
// 单例模式原理
class Demo1
{
// 目前 只能在类中的内部将类实例化
// 当前类的实例 用于实例判断
public static $instance = null;
//构造方法私有化
private function __construct()
{
}
// 实例化当前类的方法
public static function getInstance()
{
// 实例化当前类,并返回类实例/对象
// 判断当前类是否已经被实例化过了,如果没有的话就实例化,如果已实例化,就返回它
// self = static 在这里这两个都能用 在场景中用 static 不再用self
if(is_null(self::$instance))
{
// new 一个内部类
self::$instance =new self();
// $demo =new demo();
}
return self::$instance;
}
// 禁用克隆魔术方法
private function __clone()
{
// TODO: Implement __clone() method.
}
}
// 类实例化 静态实例化 Demo:: 实现 公共的 new Demo 实现
$obj3 = Demo1::getInstance();
$obj4 = Demo1::getInstance();
var_dump($obj3,$obj4);
echo ‘<br>‘;
var_dump($obj3 === $obj4);
echo ‘<hr>‘;
// 单例模型应用场景 self 全部变更为static
class Db
{
// 目前 只能在类中的内部将类实例化
// 当前类的实例
public static $pdo = null;
// 构造方法私有化
private function __construct(...$conn)
{
$dsn = $conn[0];
$username =$conn[1];
$password =$conn[2];
static::$pdo = new PDO($dsn,$username,$password);
}
// 实例化当前类的方法 传参使用
public static function getPdo(...$conn)
{
// 实例化当前类,并返回类实例/对象
// 判断当前类是否已经被实例化过了,如果没有的话就实例化,如果已实例化,就返回它
if (is_null(static::$pdo)){
// 构造函数不需要返回
new static(...$conn);
}
return static::$pdo;
}
// 禁用克隆魔术方法
private function __clone()
{
// ...
}
}
$conn = [‘mysql:host=localhost;dbname=gzg’,’root’,’root’];
$pdo = Db::getPdo(…$conn);
print_r($pdo->query(‘SELECT * FROM users
‘)->fetchAll());
`
`<?php
namespace gzg_05;
// 工厂模式: 用于批量创建类的实例/对象
use mvc\Facade;
class Demo2
{
//….
}
// file1.php
$obj = new Demo2();
// file2.php
$obj = new Demo2();
// file3.php
$obj = new Demo2();
class Test1
{
public function __construct($arg1)
{
echo ‘对象创建成功, 参数是: ‘ . $arg1;
}
}
class Test2
{
public function __construct($arg1, $arg2)
{
echo ‘对象创建成功, 参数是: ‘ . implode(‘, ‘, [$arg1, $arg2]);
}
}
class Test3
{
public function __construct($arg1, $arg2, $arg3)
{
echo ‘对象创建成功, 参数是: ‘ . implode(‘, ‘, [$arg1, $arg2, $arg3]);
}
}
// 工厂类, 专用于创建类实例
class Factory
{
public static function create($className, …$arg)
{
return new $className(…$arg);
}
}
//echo Test1::class; // _1205\Test1: 完整的类名, 带有命名空间的
// 用工厂类来创建类实例/对象
Factory::create(Test1::class,abc);
echo ‘<br>‘;
$test1 = Factory::create(Test1::class,abc);
var_dump($test1);
echo ‘<br>‘;
Factory::create(Test2::class,123,200);
echo ‘<br>‘;
Factory::create(Test3::class,aabb,123,asf);`
`<?php
namespace gzg_05;
// 依赖注入: 解决对象调用之间耦合
// 工作类
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 ‘111111111’;
}
}
// 实例化类
$person = new Person();
echo $person->work();
echo ‘<hr>‘;
/*/
//car = new Car() 在类外调用
class Person1
{
// 要依赖的外部对象
private $car = null;
// 在构造方法中将依赖的外部对象全部实例化
// 注入点放到构造方法中
public function __construct(Car $car)
{ //类外调用后传参
$this->car = $car;
}
// 外部对象执行一个动作
public function work()
{
return $this->car->drive();
}
}
// 依赖的外部类
class Car1
{
public function drive()
{
return ‘开车去上班’;
}
}
// 实例化类
//类外实例化
$car = new Car();
//给构造函数传参
$person = new Person1($car);
echo $person->work();
echo ‘<hr>‘;
`
`<?php
namespace gzg_05;
//制造商
class Maker
{
public function get ()
{
return’华为’;
}
}
<?php
namespace gzg_05;
// 商品类
class Product
{
public function get(Maker $maker)
{
return ‘该手机是由: <span style="color:green;font-weight: bolder">‘ . $maker->get() . ‘ </span>生产的’;
}
}
<?php
namespace gzg_05;
//use Closure;
use Closure;
class Container
{
// 类实例容器
protected $instance = [];
// 将类实例化的过程绑定到容器中
public function bind($alias, Closure $process)
{
$this->instance[$alias] = $process;
}
// 取出保存在容器中的实例化过程的闭包,并执行它
public function make($alias)
{
return $this->instance[$alias]();
}
}
<?php
namespace gzg_05;
use http\Client;
require ‘Product.php’;
require ‘Maker.php’;
// 先不用容器
class Client1
{
public function show()
{
$product = new Product();
$make = new Maker();
return $product->get($make);
}
}
$obj = new Client1();
echo $obj->show();
echo ‘<br>‘;
echo ((new Client1())->show());
<?php
namespace gzg_05;
require ‘Product.php’;
require ‘Maker.php’;
require ‘Container.php’;
class Client2
{
// 输出商品与制造商
public function show( Product $product,Maker $maker)
{
// 制造商注入到产品类中
return $product->get($maker);
}
}
// 客户端调用
// 将类实例绑定到容器中并实例化且返回
$container= new Container();
//$container->bind(‘product’,function (){return new Product();});
//$container->bind(‘Maker’,function (){return new Maker();});
$container->bind(‘product’, function () {return new Product();});
$container->bind(‘maker’, function () {return new Maker();});
// 创建类实例并返回
$product = $container->make(‘product’);
$maker = $container->make(‘maker’);
echo (new Client2())->show($product,$maker);
`
<?php
spl_autoload_register(function ($className){
$path = str_replace('\\', '/', $className);
require dirname(__DIR__) . DIRECTORY_SEPARATOR . $path . '.php';
});
`<?php
//旅游类 传统方式
namespace base;
//设置引用的外部类名的别名 把类名全部改为小写 不改别名的话 调用时为 Car 类的实际名称
use base\inc\Car as car;
use base\inc\Train as train;
use base\inc\Plane as plane;
//require DIR . ‘/inc/Car.php’;
//$path = str_replace(‘\‘, ‘/‘, \inc\Car::class);
//echo $path, ‘<br>‘;
require DIR . ‘/autoload.php’;
class Travel10
{
// 交通工具
private $vehicle;
//构造方法
public function __construct($vehicle)
{
// print_r($vehicle);
// exit;
//strtolower() 把参数全部改为小写
// 根据参数进行不同的选择,调用不同的类
switch (strtolower($vehicle)){
case 'car':
$this->vehicle = new Car();
break;
case 'train':
$this->vehicle = new train();
break;
case 'plane':
$this->vehicle = new plane();
}
}
// 调用外部一个依赖对象
public function travelModel()
{
return $this->vehicle->drive() . ' : 去旅行';
}
}
// 客户端调用
// $car = new Travel10(‘car’);
// echo $car->travelModel();
// echo ‘<br>‘;
echo (new Travel10(‘car’))->travelModel(), ‘<br>‘;
//echo ‘111’;
//$car = new Car();
//echo $car->drive();
echo (new Travel10(‘train’))->travelModel(), ‘<br>‘;
echo (new Travel10(‘plane’))->travelModel(), ‘<br>‘;
<?php
namespace base;
use base\inc\Train as train;
use base\inc\Car as car;
use base\inc\Plane as plane;
require DIR . ‘/autoload.php’;
//$path = str_replace(‘\‘, ‘/‘, \inc\Car::class);
//echo $path, ‘<br>‘;
// 工厂类, 专用于创建类实例
class Factory1
{
protected static $instance =null;
public static function getInstance($vehicle)
{
switch (strtolower($vehicle))
{
case ‘car’:
static::$instance = new car();
break;
case ‘train’:
static::$instance = new train();
break;
case ‘plane’:
static::$instance = new plane();
}
// 返回当前具体的交通工具
return static::$instance;
}
}
class Travel5
{
// 交通工具
private $vehicle;
//构造方法
public function __construct($vehicle)
{
echo $vehicle;
$this->vehicle = Factory1::getInstance($vehicle);
}
public function travelModel()
{
return $this->vehicle->drive() .'1111111';
}
}
// 客户端调用
$car = new Travel5(‘car’);
echo $car->travelModel();
echo ‘<br>‘;
echo (new Travel5(‘train’))->travelModel(),’<br>‘;
echo (new Travel5(‘plane’))->travelModel(),’<br>‘;
<?php
namespace base;
use base\inc1\Car ;
use base\inc1\iVehicle;
use base\inc1\Train;
use base\inc1\Plane;
use base\inc1\Ship;
//
require DIR . ‘/autoload.php’;
//$path = str_replace(‘\‘, ‘/‘, Car1::class);
//echo $path, ‘<br>‘;
//$path = str_replace(‘\‘, ‘/‘, \inc1\iVehicle1::class);
//echo $path, ‘<br>‘;
//exit;
class Travel8
{
// 交通工具
private $vehicle;
// 构造方法
public function __construct(iVehicle $vehicle)
{
$this->vehicle = $vehicle;
}
// 调用外部一个依赖对象
public function travelModel()
{
return $this->vehicle->drive() . ' : ======= 去旅行';
}
}
// $car1 = new Car();
//echo (new Travel8($car))->travelModel(), ‘<br>‘;
//// $car1 = new Trave8($car) ;
//
$car = new Car();
echo (new Travel8($car))->travelModel(), ‘<br>‘;
echo (new Travel8(new Plane()))->travelModel(), ‘<br>‘;
echo (new Travel8(new Ship()))->travelModel(), ‘<br>‘;
$ship1 = new Ship();
$ship2 = new Travel8( $ship1);
echo $ship2->travelModel();
echo ‘<br>‘;
$ship = new Ship();
$ship1 = new Travel8( $ship);
echo $ship1->travelModel();
echo ‘<br>‘;
$ship1 = new Travel8( new $ship);
echo $ship1->travelModel();
echo ‘<br>‘;`
接口 把面向对象改为面向接口。全部由接口调用
容器 了解了容器的原理,容器就是管理 对象的。