note 23800 added to functions
| From: | php-general at lists dot php dot net | Date: | Mon, 29 Jul 2002 08:10:51 +0000 |
| Subject: | note 23800 added to functions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-33853@lists.php.net to get a copy of this message | ||
PHP's variable scope is a very useful feature... if you feel the need to declare variables
inside functions global, then there's surely a better way to do what you want to do. A function
should, like an Object, know as little as possible about its calling environment.
An example here was to have a function set certain configuration variables. However instead, why not
return an associative array? Like this:
-------
function set_config() {
return array("items_per_page" => 10, "allow_html" => 0);
}
$conf = set_config();
-------
This is a flexible solution because you can replace this simple piece of code with one that fetches
the variable's values from a database or a file, without having to change the code that calls
the function.
PHP has some very useful language features that often seem alien to people coming from a JavaScript
or VB background, but rather than seeing them as 'bad design decisions', think of their
cool possibilities and uses.
--
http://www.php.net/manual/en/functions.php
http://master.php.net/manage/user-notes.php?action=edit+23800
http://master.php.net/manage/user-notes.php?action=delete+23800
http://master.php.net/manage/user-notes.php?action=reject+23800