compat.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2007 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * Think兼容函数库 针对5.2.0以下版本
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Common
  18. * @author liu21st <liu21st@gmail.com>
  19. * @version $Id$
  20. +------------------------------------------------------------------------------
  21. */
  22. if (!function_exists('json_encode')) {
  23. function format_json_value(&$value)
  24. {
  25. if(is_bool($value)) {
  26. $value = $value?'true':'false';
  27. }elseif(is_int($value)) {
  28. $value = intval($value);
  29. }elseif(is_float($value)) {
  30. $value = floatval($value);
  31. }elseif(defined($value) && $value === null) {
  32. $value = strval(constant($value));
  33. }elseif(is_string($value)) {
  34. $value = '"'.addslashes($value).'"';
  35. }
  36. return $value;
  37. }
  38. function json_encode($data)
  39. {
  40. if(is_object($data)) {
  41. //对象转换成数组
  42. $data = get_object_vars($data);
  43. }else if(!is_array($data)) {
  44. // 普通格式直接输出
  45. return format_json_value($data);
  46. }
  47. // 判断是否关联数组
  48. if(empty($data) || is_numeric(implode('',array_keys($data)))) {
  49. $assoc = false;
  50. }else {
  51. $assoc = true;
  52. }
  53. // 组装 Json字符串
  54. $json = $assoc ? '{' : '[' ;
  55. foreach($data as $key=>$val) {
  56. if(!is_null($val)) {
  57. if($assoc) {
  58. $json .= "\"$key\":".json_encode($val).",";
  59. }else {
  60. $json .= json_encode($val).",";
  61. }
  62. }
  63. }
  64. if(strlen($json)>1) {// 加上判断 防止空数组
  65. $json = substr($json,0,-1);
  66. }
  67. $json .= $assoc ? '}' : ']' ;
  68. return $json;
  69. }
  70. }
  71. if (!function_exists('json_decode')) {
  72. function json_decode($json,$assoc=false)
  73. {
  74. // 目前不支持二维数组或对象
  75. $begin = substr($json,0,1) ;
  76. if(!in_array($begin,array('{','[')))
  77. // 不是对象或者数组直接返回
  78. return $json;
  79. $parse = substr($json,1,-1);
  80. $data = explode(',',$parse);
  81. if($flag = $begin =='{' ) {
  82. // 转换成PHP对象
  83. $result = new stdClass();
  84. foreach($data as $val) {
  85. $item = explode(':',$val);
  86. $key = substr($item[0],1,-1);
  87. $result->$key = json_decode($item[1],$assoc);
  88. }
  89. if($assoc)
  90. $result = get_object_vars($result);
  91. }else {
  92. // 转换成PHP数组
  93. $result = array();
  94. foreach($data as $val)
  95. $result[] = json_decode($val,$assoc);
  96. }
  97. return $result;
  98. }
  99. }
  100. if (!function_exists('property_exists')) {
  101. /**
  102. +----------------------------------------------------------
  103. * 判断对象的属性是否存在 PHP5.1.0以上已经定义
  104. +----------------------------------------------------------
  105. * @param object $class 对象实例
  106. * @param string $property 属性名称
  107. +----------------------------------------------------------
  108. * @return boolen
  109. +----------------------------------------------------------
  110. */
  111. function property_exists($class, $property) {
  112. if (is_object($class))
  113. $class = get_class($class);
  114. return array_key_exists($property, get_class_vars($class));
  115. }
  116. }
  117. ?>