Req #72382 [Opn->Dup]: THIS SHOULD BE CONSIDERED A BUG: __toString() must not throw an exception

From: Date: Sat, 11 Jun 2016 04:37:34 +0000
Subject: Req #72382 [Opn->Dup]: THIS SHOULD BE CONSIDERED A BUG: __toString() must not throw an exception
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201536@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72382&edit=1 ID: 72382 Updated by: requinix@php.net Reported by: travis dot detert at gmail dot com Summary: THIS SHOULD BE CONSIDERED A BUG: __toString() must not throw an exception -Status: Open +Status: Duplicate Type: Feature/Change Request -Package: PHP Language Specification +Package: Class/Object related Operating System: All OS PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Duplicate of request #53648. Previous Comments: ------------------------------------------------------------------------ [2016-06-11 04:27:07] travis dot detert at gmail dot com Description: ------------ The fundamental behavior of "__toString() must not throw an exception" needs to be changed. This problem is completely antithetical to object oriented design, and just doesn't even make sense. class Template{ function __construct($path){ $this->path = $path; } function __toString(){ if(!file_exists($this->path)){ throw new Exception('Path does not exist'); }else{ //process template } } $Template = new Template('/path/does/not/exist.php'); echo (string)$Template; There is absolutely no sane reason that this should be a fatal php error. Test script: --------------- class Template{ function __construct($path){ $this->path = $path; } function __toString(){ if(!file_exists($this->path)){ throw new Exception('Path does not exist'); }else{ //process template } } $Template = new Template('/path/does/not/exist.php'); echo (string)$Template; Expected result: ---------------- Raise a regular exception. Why would raising an exception ever be not allowed? What is the point? Actual result: -------------- __toString() must not throw an exception Pretty much the worst result possible. Unrecoverable fatal error. Why? What is the point? ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72382&edit=1

« previous php.bugs (#201536) next »