Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We've seen how to set the properties of an existing PHP object from the result of a select query. PDO can also create a new object and set its properties, but you need to be careful that default values in the object constructor don't overwrite the values from the database result. We'll use the same Car class as in the previous video. And the script in fetch_class.php is also very similar. The only difference is that we're not actually creating an object beforehand.
We're going to use the results of the database query to create our new object. Incidentally, you can find both of these files in the chapter four, 04_03 folder of the exercise files. So, we've got a prepared statement that is selecting values from the cars database and it's using car_id. That's got a question mark as the placeholder. We then prepare the statement and then we execute the statement, parsing at the value of car_id, which is bound to that placeholder. So, we can now use the database result to create an instance of the object that we want. First, we need to set the fetch mode on the statement. So let's add some new code here. We need the statement object and the setFetchMode method. The first argument that we need to pass is a PDO constant. This time, it's PDO, double colon, and its fetch class, FETCH_CLASS. The second argument is the name of the class definition as a string, so it's Car.
And the third argument is an array of any values that need to be passed as arguments to the class constructor. There's just one value we need to pass. That's the car ID, so we need an array with a single element in it, which is car_id. Then we can call the fetch method and assign the result to the object that we're creating from the select query. So we'll call our object car, statement fetch. So car is now our new object that has been created using our database query.
Because the class uses the magic toString method, we can inspect it using echo. We'll just car, and then if we save that, and load the page into a browser. We've created our object, but probably that's not the result that you were expecting. It's got the correct car ID, but it's got all of the default values instead of the values that have been drawn from the database. And the reason that's happened is that the PDO fetch class mode calls the constructor after populating the properties with the database result. Consequently, the default values overwrite the ones that we want to use. And that's part of the problem. It's using default values in the constructor. Let's go back to the editing program and see what happens if we comment out these default values here in the car constructor.
We just save that. Go back to the browser and refresh. We now get the values that have been drawn from that database query. Well, that works, but it's not very practical. You don't want to have to comment out default values in your class constructors. So let's go back and see how we can fix this. First, we'll uncomment those and save that, and then go back to fetch_class.php. What we need to do is combine this PDO FETCH_CLASS constant with another constant. So, put the cursor immediately before the comma, then type a vertical pipe. And the other one is PDO, and then two colons and its FETCH, then underscore PROPS_LATE.
There we are. And what this does is it calls the class constructor first and then uses the properties that have been drawn from the database to set various properties within the new object. So if we save that, go back to the browser. If we refresh, we should still have those values there. And we can just check it by going back and changing to a different ID. Let's choose 25. Save that. Refresh. And now we're getting the correct values that we want.
So to create an instance of an object from a database result, mapping the columns to properties of the same name in the class, you need to set the fetch mode to PDO FETCH_CLASS. The second argument is the class name. And the third argument is an array of arguments to pass to the class constructor. But used on its own, PDO FETCH_CLASS calls the constructor after setting the object's properties. So default properties overwrite those obtained from the database result. To call the constructor first, you need to combine PDO FETCH_CLASS and PDO FETCH_PROPS_LATE.
And you combine two constants like this by separating them with a vertical pipe. One final point, the class definition needs to use the magic set method to set the property values. It won't work with custom setter methods.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100051 Viewers
56 Video lessons · 113110 Viewers
71 Video lessons · 81964 Viewers
131 Video lessons · 39323 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.