Comm.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of Com
  9. *
  10. * @author jgao
  11. */
  12. class Comm {
  13. //put your code here
  14. /**
  15. * 对象克隆
  16. * @param type $obj
  17. */
  18. static function cloneObject($obj, $type=null){
  19. if($type != null){
  20. $clone = new $type();
  21. }else{
  22. $clone = new stdClass();
  23. }
  24. foreach ($obj as $key => $value) {
  25. $clone->$key = $value;
  26. }
  27. return $clone;
  28. }
  29. /**
  30. * 对象装箱
  31. * @param type $surObj 原始obj
  32. * @param type $desObj 具体obj
  33. */
  34. static function loadObject($surObj, &$desObj) {
  35. if($surObj==null||$desObj==null) return;
  36. foreach ($desObj as $key => $value) {
  37. if (property_exists($surObj, $key)) {
  38. $desObj->$key = $surObj->$key;
  39. }
  40. }
  41. }
  42. /**
  43. * 对象装箱
  44. * @param type $obj
  45. * @param type $type
  46. * @return type
  47. */
  48. static function loadClass($obj, $type){
  49. $newObj = new $type();
  50. self::loadObject($obj, $newObj);
  51. return $obj;
  52. }
  53. /**
  54. * 数组装箱
  55. * @param type $obj
  56. * @param type $type
  57. * @return type
  58. */
  59. static function loadArray($obj, $type){
  60. $array = array();
  61. foreach($obj as $item){
  62. $array[] = self::loadClass($item, $type);
  63. }
  64. return $array;
  65. }
  66. /**
  67. * 字典装箱
  68. * @param type $obj
  69. * @param type $type
  70. * @return \stdClass
  71. */
  72. static function loadDict($obj, $type){
  73. $dict = new stdClass();
  74. foreach($obj as $key=>$val){
  75. $dict->$key = self::loadClass($val, $type);
  76. }
  77. return $dict;
  78. }
  79. /**
  80. * 方法调用
  81. * @param type $func
  82. * @param type $paras
  83. * @param type $class
  84. */
  85. static function callFunction($func, $paras, $class=""){
  86. if($class == "" || $class == null){
  87. call_user_func_array($func, $paras);
  88. }else{
  89. call_user_func_array(array($class, $func), $paras);
  90. }
  91. }
  92. /**
  93. * 浮点数转整形
  94. * @param type $value
  95. * @return type
  96. */
  97. static function floatToInt($value){
  98. return round($value - 0.4999);
  99. }
  100. /**
  101. * 浮点数转上整形
  102. * @param type $value
  103. * @return type
  104. */
  105. static function floatToCeil($value){
  106. return round($value + 0.4999);
  107. }
  108. /**
  109. * 解析异常描述信息
  110. * @param type $e
  111. * @return type
  112. */
  113. static function getExceptMsg($e) {
  114. return 'Msg:'.$e->getMessage().' Code:'.$e->getCode().' File:'.$e->getFile().' Line:'.$e->getLine();
  115. }
  116. /**
  117. * 提取url参数
  118. * @param type $querryStr
  119. * @return type
  120. */
  121. static function getQuerryParas($querryStr) {
  122. $arr = explode("&", $querryStr);
  123. $querryParas = array();
  124. foreach ($arr as $value) {
  125. $paras = explode("=", $value);
  126. $querryParas[$paras[0]] = $paras[1];
  127. }
  128. return $querryParas;
  129. }
  130. /**
  131. * 获取客户端ip地址及端口
  132. * @return type
  133. */
  134. static function getClientEP(){
  135. return $_SERVER['REMOTE_ADDR'].":".$_SERVER['REMOTE_PORT'];
  136. }
  137. }