PHP如何获取PagSeguro返回的Response对象中的私有code属性
获取PagSeguro返回对象私有属性的方法
官方推荐方案(优先使用)
PagSeguro SDK的PagSeguro\Parsers\Transaction\Response类为所有私有属性预置了公开的Getter方法,你要获取的code属性直接调用getCode()方法即可:
// $result 为你调用接口返回的响应对象 $transactionCode = $result->getCode();
同理打印结果里的date、reference属性也可以通过getDate()、getReference()方法直接获取。
备用方案(不推荐)
如果遇到特殊场景无法使用官方Getter,可以通过PHP反射机制强制读取私有属性,该方案强依赖SDK内部实现,SDK版本更新后可能失效:
$reflection = new \ReflectionClass($result); $codeProp = $reflection->getProperty('code'); $codeProp->setAccessible(true); $transactionCode = $codeProp->getValue($result);
内容的提问来源于stack exchange,提问作者pedrogcs
相关产品推荐
相关产品推荐

