note 23800 deleted from functions by dams

From: Date: Tue, 30 Jul 2002 00:30:02 +0000
Subject: note 23800 deleted from functions by dams
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-33917@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.

« previous php.notes (#33917) next »