|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2016-06-11 04:37 UTC] requinix@php.net
-Status: Open
+Status: Duplicate
-Package: PHP Language Specification
+Package: Class/Object related
[2016-06-11 04:37 UTC] requinix@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 22:00:02 2025 UTC |
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?