Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
I can also create a standard object straight from the database. One of the methods in MySQLi, fetch_ object, returns the current row of a result set as a standard class object. Let's add this to the address model. Let's go to the load function, which is currently a stub. I am going to add a connection to the database. Connect to the database, then write the query to retrieve a row from the address table by the primary key, address_id.
For safety, I'm using quotations around the identifier, and casting the identifier as an integer. sql_query equals SELECT everything from address, from address where address_id equals (int) $address_id.
The $result = mysqli ->query($sql_query). Use the mysqli result object to fetch an object from the database. If ($row = $result -> fetch_object. Display the row, then exit var_dump ($row) exit. Save, then open the demo script.
Remove the test object code, then add a title section for testing loading,
followed by a call to the static method load in the address class.
There is one test row in the address table, so explicitly load using the first
Loading from database, $address_db = Address:(1). Then, debug the
Save, then switch to the browser, and reload. The object returned from the database is a standard object. MYSQLI can also populate objects if you pass the object name as a parameter. But as there is potentially data for multiple classes in this one table, you will not be able to use that. Instead switch MYSQLI's fetch to an associative array. fetch_assoc.
In order to automatically populate address objects, I'm going to define a public static helper method. It will get an instance of a particular class based on the address type ID, then populate it with an array. I'll make it public, as I can reuse this method elsewhere, as it's useful to be able to create addresses without having to explicitly specify the subclass. final public static function getInstance starts with an address type_id, and takes data, which is an array. Add documentation, given an address type_id. Return an instance of that subclass param int $address type_id @param array $data, and then return address, and it will add a note that it will be the sub class.
As all the address classes start with the address, start the class name with the address. Then, using the static property containing the array of address types, get the name of the type of address. $class_name equals address, followed by self:: valid_address_types [$address_type_id]; You can use the new keyword with a variable name. Just follow it with parentheses containing the data for the constructor. Return new $class_name. And, we'll pass the data.
Now that you have a mechanism to return a class of any address type, return to the load method. Instead of dumping the contents and exiting, call the getInstance method. Remember, you can't use this in a static context. Return self::getInstance ($row ['address type id'], followed by the whole row itself.
Before testing this out, a small change needs to be made to the constructor. When it was initially written, it supported the protected properties, time created and time updated. Since that point, additional protected properties were added, but the constructor did not account for them, as there were no records coming from the database yet. Then add two new lines. address_id and address type_id. These will be prepended with an underscore for the protected properties. Save, then go to your browser, and refresh the page.
The loader and helper functions have chosen the correct class for the row. In the next video, I will demonstrate how to intentionally create and respond to object-oriented errors in PHP.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99127 Viewers
56 Video lessons · 112408 Viewers
71 Video lessons · 81230 Viewers
131 Video lessons · 39009 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.