PHP 之 代理模式

简介:

代理模式是很好用的,不过我们经常用JS来实现一些图片的懒加载,而且现在有很多继承好的js


对于PHP的,肯定不仅仅限于图片,不过这次的例子还是PHP的图片代理,是可以直接显示图片的,修改下路径就好。


应用情境:1.图片代理,2.远程代理,3.智能指引,4.虚拟代理,5.动态代理


一般是开启多线程。代理对象中一个线程向客户端浏览器加载一个小图片,第二个线程调用大图片加载程序第三个线程,当用户浏览大图还没有加载出来就显示 相应的提示信息  (这个示例没有利用线程

这样的话就完全将加载图片放在了后台,同样处理其他的业务也是可以借鉴


上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
     //多用于功能列表,继承一些公用的接口函数
     interface  Image{
         public  function  getWidth();
         public  function  getHeight();
         public  function  getPath();
 
     /**  
      * @return string   the image's byte stream  
      */ 
         public  function  dump();
     }
     //可以多体会下抽象对象的用法,不能实例化
     abstract  class  AbstractImage  implements  Image{
         protected  $_width ;
         protected  $_height ;
         protected  $_path ;
         protected  $_data ;
         protected  $_type ;
 
         public  function  getWidth(){
             return  $this ->_width;
         }
 
         public  function  getHeight(){
             return  $this ->_height;
         }
 
         public  function  getPath(){
             return  $this ->_path;
         }
 
         
     }
 
         //具体的实体对象 继承抽象类对于接口的重写
         //可以直接使用抽象对象的通用属性width,height,path,data
         //包括可以直接重新定义接口里的函数
         //这是实际的图片对象
     class  RawImage  extends  AbstractImage{
         public  function  __construct( $path ){
             $this ->_path =  $path ;
             //list() 函数用数组中的元素为一组变量赋值。按照数组的数字索引 依次赋值
             //注意,与 array() 类似,list() 实际上是一种语言结构,不是函数。
             list( $this ->_width, $this ->_height) =  getimagesize ( $path );
             $this ->_type =  getimagesize ( $path )[ 'mime' ];
             //file_get_contents() 函数把整个文件读入一个字符串中。
             //和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。
             //file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
             $this ->_data =  file_get_contents ( $path );
         }
         
         public  function  dump_type(){
             return  $this ->_type;
         }
 
         public  function  dump(){
             return  $this ->_data;
         }
     }
     //它和实际的图片对象继承同一个抽象接口,基本上就是同样的
     //这时候就可以增加很多人性化的功能,与图片无关,与用户体验有关
     class  ImageProxy  extends  AbstractImage{
         protected  $_realImage ;
 
         public  function  __construct( $path ){
             $this ->_path =  $path ;
             list( $this ->_width, $this ->_height) =  getimagesize ( $path );
             $this ->_type =  getimagesize ( $path )[ 'mime' ];
             //这里就没必要获取图片的真实数据,毕竟很大
         }
 
      /**  
      * Creates a RawImage and exploits its functionalities.  
      */
      //这里去获取真实图片的所有数据
         protected  function  _lazyLoad(){
             if ( $this ->_realImage === null){
                 $this ->_realImage =  new  RawImage( $this ->_path);
             }
         }
 
         public  function  dump_type(){
             return  $this ->_type;
         }
 
         public  function  dump(){
             $this ->_lazyLoad();
             return  $this ->_realImage->dump();
         }
     }
 
 
 
     //基本上一个很简单的代理写完了,如何发挥更多的效用,需要好好引进去很多处理思路,但是位置一定是写在代理里面
     //下面就是客户端类
     class  Client{
         public  function  tag(Image  $img ){
              $type = $img ->dump_type();
              header( "content-type:$type" );
              echo  $img ->dump();
         }
     }
 
     $path  'd:/image/timg3.jpg' ;
     $client  new  Client();
 
 
     $image  new  ImageProxy( $path );
     //$image = new RawImage($path);
     $client ->tag( $image );
?>



愿法界众生,皆得安乐。

本文转自 jackdongting 51CTO博客,原文链接:http://blog.51cto.com/10725691/1954719


相关文章
|
30天前
|
PHP
深入理解PHP中的面向对象编程
【2月更文挑战第29天】在这篇文章中,我们将深入探讨PHP中的面向对象编程。面向对象编程是现代编程的基础之一,它允许我们创建复杂的应用程序并管理大量的代码。本文将介绍PHP面向对象编程的基本概念,包括类、对象、属性和方法,以及如何在实际项目中应用这些概念。
|
9月前
|
PHP
PHP 代理
PHP 代理
|
10月前
|
缓存 Java PHP
如何理解什么是PHP接口,如何应用PHP接口?
如何理解什么是PHP接口,如何应用PHP接口?
PHP-FPM和PHP的关系是什么?底层原理是什么?
PHP-FPM和PHP的关系是什么?底层原理是什么?
239 0
|
关系型数据库 MySQL 数据库连接
php-工厂模式
php-工厂模式
56 0
|
存储 PHP
php-单例模式
php-单例模式
72 0
php-单例模式
|
PHP 开发者
PHP 继承特点|学习笔记
快速学习 PHP 继承特点,PHP 的面向对象并不一样,它的技巧也有区别。接下来学习 PHP 的继承特点,其实主要是为了避免在使用过程中出现一些理所应当的问题,而实际上要基于 PHP 实际上的特点进行开发。
70 0
|
存储 运维 PHP
PHP 继承总结|学习笔记
快速学习 PHP 继承总结
76 0
|
存储 自然语言处理 PHP
PHP类的原理
类的内部存储结构
PHP类的原理
|
PHP
PHP类(初学)
类示例:Cat.php
112 0