经由过程检察 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相干学问,请接见PHP教程!
以上就是php经由过程反射修正Exception实例的message属性的细致内容,更多请关注ki4网别的相干文章!