1--TEST-- 2Exception with delayed message computation 3--FILE-- 4<?php 5 6class MyException extends Exception { 7 public $message; 8 public $messageCallback; 9 10 public function __construct() { 11 $this->messageCallback = static function() { 12 return "Foobar"; 13 }; 14 $this->message = new class($this->message, $this->messageCallback) { 15 private $message; 16 private $messageCallback; 17 18 public function __construct(&$message, &$messageCallback) 19 { 20 $this->message = &$message; 21 $this->messageCallback = &$messageCallback; 22 } 23 24 public function __toString(): string 25 { 26 $messageCallback = $this->messageCallback; 27 $this->messageCallback = null; 28 return $this->message = $messageCallback(); 29 } 30 }; 31 } 32} 33 34throw new MyException; 35 36?> 37--EXPECTF-- 38Fatal error: Uncaught MyException: Foobar in %s:%d 39Stack trace: 40#0 {main} 41 thrown in %s on line %d 42