首页 > Web开发 > 详细

php适配器设计模式

时间:2016-08-23 14:41:19      阅读:145      评论:0      收藏:0      [点我收藏+]
<?php 
    //适配器模式
    //服务器端代码
     class tianqi{
        public static function show(){
            $today= array(‘tep‘ =>28 ,‘wind‘=> 7,‘sun‘=>‘sunny‘);
            return serialize($today);
        }
    }
    //增加一个适配器,转换为json格式的数据
     class AdapterTianqi extends tianqi{
          public static function show(){
              $today=parent::show();
              $today=unserialize($today);
              $today=json_encode($today);
              return $today;
          }
     }
    //客户端调用
     $tq=unserialize(tianqi::show());
     echo ‘温度:‘,$tq[‘tep‘],‘<br>‘;
     echo ‘风力:‘,$tq[‘wind‘],‘<br>‘;
     echo ‘sun:‘,$tq[‘sun‘],‘<br>‘;
    //来了一批手机上的java客户端,不认识php的串行化后的字符串,怎么办?
     $tq=AdapterTianqi::show();
     $tq=json_decode($tq);

     echo "温度:",$tq->tep,‘<br>‘;
     echo "风力:",$tq->wind,‘<br>‘;
     echo "sun:",$tq->sun,‘<br>‘;

 ?>

 

php适配器设计模式

原文:http://www.cnblogs.com/kangshuai/p/5799158.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!