1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace loyalsoft;
- /**
- * Msgpack转换工具
- * @author gwang
- * @version: <br/>
- * v1.0.0 file copy and created. gwang 2024.9.28 <br/>
- */
- class MsgpackUtil {
-
- public $name;
- public function Test(){
-
- var_dump(nameof($this));
- var_dump(nameof( $this->name));
- }
- /**
- * obj -> bytes
- * @param mixed $obj
- * @return string
- */
- public static function encode($obj) {
- $bytes = \msgpack_pack($obj);
- return $bytes;
- }
- /**
- * bytes -> obj
- * @param type $string
- * @return obj
- */
- public static function decode($string) {
- $obj = \msgpack_unpack( $string);
- return $obj;
- }
- /**
- * 研发中..., 想着直接一步到位, 反序列化成目标类型的对象. -by gwang 2017年8月8日 09:49:09
- * @param string $str json字符串
- * @param mixed $tarType 目标类型
- * @return obj
- */
- public static function decode2Object($str, $tarType = null) {
- $obj = self::decode($str);
- if ($tarType != null) {
- $c = 'stdClass';
- if (is_string($tarType)) { # 类型名称
- $c = $tarType;
- } else if (is_object($tarType)) { # 目标类型的对象,数据还不能处理
- $c = get_class($tarType);
- }
- $desObj = new $c;
- CommUtil::loadObject($obj, $desObj); # 装到目标类型的箱子中
- $obj = $desObj;
- }
- return $obj;
- }
- }
|