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

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Set the database

Set the database - PHP Tutorial

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

Start my 1-month free trial

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 approach is just to tell this class about its database connection. So…

Contents