Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Historical overview of object-oriented PHP
- Defining classes
- Creating a method/object context with $this
- Accessing classes without instantiation
- Creating a database class
- Extending and abstracting classes
- Cloning and comparing objects
- Error handling with exceptions
- Implementing design patterns, such as the factory and strategy patterns
Skill Level Intermediate
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 row. echo <h2>Loading from database, $address_db = Address:(1). Then, debug the address_db variable.
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.