Fantastic! A technique to avoid using global variables, but while still keeping all the problems associated with global variables!
The problem with using global variables is not an irrational fear of the our keyword. The problem is that it introduces global state. This means that if you write an app that, say, operates a blog, and has a database handle as part of its global state, it becomes very difficult to reuse any of that code when you want to write an app that manages multiple blogs (across multiple databases). All your functions are picking up the same database handle from some global place, so you can't tell one function that it needs to copy an article from blog A, and another function that the same article needs to be pasted into blog B.
Global state makes testing very difficult. If function do_something isn't passed a copy of $dbh as an argument, but instead picks it up from global state, then it becomes difficult to test do_something by passing it test/dummy database handles.
So the enemy is not global variables. The enemy is global state. Global variables are a manifestation of global state, but so are the techniques you suggest above.
In reply to Re^2: Best practices passing database handles, cgi objects, etc.
by tobyink
in thread Best practices passing database handles, cgi objects, etc.
by xtpu2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |