Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP User Authentication Building the Library Building the Book List

Is not a bad practice to use globals inside function's scope?

From what I know, using global variables inside function's scope creates a lot of problems, I wonder, why are you using this way of working with function's dependencies? Passing the $db variable as a parameter makes more sense.

1 Answer

Brian Retterer
STAFF
Brian Retterer
Treehouse Guest Teacher

Hi Pirvan, Yes, there are better ways for accessing the $db variable and we chose to do it this way in the lessons to not have more complexity. The best way to do this would be creating a Database class that holds all of the database functionality and then creating a new instance or using an already instantiated version of that Database class for all functions that need to make CRUD requests.