note 114304 added to function.return

From: Date: Wed, 05 Feb 2014 10:36:00 +0000
Subject: note 114304 added to function.return
Groups: php.notes 
Request: Send a blank email to php-notes+get-198411@lists.php.net to get a copy of this message
Keep in mind that even if PHP allows you to use "return" in the global scope it is very bad design to do so. Using the return statement in the global scope encourages programmers to use files like functions and treat the include-statement like a function call. Where they initialize the file's "parameters" by setting variables in the global scope and reading them in the included file. Like so: (WARNING! This code was done by professionals in a controlled environment. Do NOT try this at home!) <?php $parameter1 = "foo"; $parameter2 = "bar"; $result = include "voodoo.php"; ?> Where "voodoo.php" may be something like: <?php return $parameter1 . " " . $parameter2; ?> This is one of the worst designs you can implement since there is no function head, no way to understand where $parameter1 and $parameter2 come from by just looking at "voodoo". No explanation in the calling file as of what $parameter1 and -2 are doing or why they are even there. If the names of the parameters ever change in "voodoo" it will break the calling file. No IDE will properly support this very poor "design". And I won't even start on the security issues! If you find yourself in a situation where a return-statement in global scope is the answer to your problem, then maybe you are asking the wrong questions. Actually you may be better off using a function and throwing an exception where needed. Files are NOT functions. They should NOT be treated as such and under no circumstances should they "return" anything at all. Remember: Every time you abuse a return statement God kills a kitten and makes sure you are reborn as a mouse! ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/function.return.php Edit -- https://master.php.net/note/edit/114304 Del: integrated -- https://master.php.net/note/delete/114304/integrated Del: useless -- https://master.php.net/note/delete/114304/useless Del: bad code -- https://master.php.net/note/delete/114304/bad+code Del: spam -- https://master.php.net/note/delete/114304/spam Del: non-english -- https://master.php.net/note/delete/114304/non-english Del: in docs -- https://master.php.net/note/delete/114304/in+docs Del: other reasons-- https://master.php.net/note/delete/114304 Reject -- https://master.php.net/note/reject/114304 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198411) next »