From the course: PHP: Object-Oriented Programming with Databases

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Set the database

Set the database

- [Instructor] I want to start implementing the active record design pattern by first telling our object about the database connection that it should be using. In the code that we've already implemented where we're using a database connection, you can see right here that we're calling database query. So this is just simply inside a PHP file. This is not inside a class. What I want to do is I want to be able to move queries like this inside my class, and to do that I need to tell the bicycle class about the database connection. Now I could come over here to my bicycle class and I could do something like use global in order to bring in that variable so that we could use it inside our class. But that's not ideal for a couple of reasons. It means that every time I have a method that's going to use the database, I have to first remember to tell that method to bring in the database variable from the global scope. A better…

Contents