note 114304 deleted from function.return by peehaa

From: Date: Sun, 28 Apr 2019 10:29:05 +0000
Subject: note 114304 deleted from function.return by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-210790@lists.php.net to get a copy of this message
Note Submitter: Tom ---- 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!

« previous php.notes (#210790) next »