php中Exception实例应该怎么修改message?
通过查看 Exception 类的源码可以知道, $message 属性使用 protect 修饰, 且没有提供 setMessage 方法。
对于 Exception 实例应该怎么修改 message 呢?答案是: 反射!
$exception = new \Exception('haha');$message = " - use reflection appended message";$reflectionObject = new \ReflectionObject($exception);$reflectionObjectProp = $reflectionObject->getProperty('message');$reflectionObjectProp->setAccessible(true);$reflectionObjectProp->setValue($exception, $exception->getMessage() . $message);print_r($exception->getMessage());haha - use reflection appended message
通过以上代码,能把 $exception 中的 $message 修改掉!反射无敌。。。
以上就是php通过反射修改Exception实例的message属性的详细内容,更多请关注亿速云其它相关文章!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。