Retrieving objects from the database
Video: Retrieving objects from the databaseI 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.
Viewers: in countries Watching now:
Whether you're enhancing or optimizing existing code or just starting from scratch, there's never a better time to start integrating object-oriented design techniques. This course shows how to integrate the principles of object-oriented programming into the build of a PHP-driven web page or application. After an overview of what objects and classes are and why they should be used, author Jon Peck dives into creating and instantiating objects, then defining the class relationships and interactions that will form the basis of your coding arsenal. The course also shows how to leverage PHP objects and implement design patterns, and looks at steps you can take to continue adding to your programming tool belt.
- 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
Retrieving objects from the database
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.
Find answers to the most frequently asked questions about Object-Oriented Programming with PHP .
Here are the FAQs that matched your search "" :
- Q: I'm not seeing warnings or errors in my environment like the video; why not?
- A: Your PHP configuration is probably configured not to show them to you. This is often true on commercial web hosts and is often the default. Fortunately, there are multiple ways of resolving this.
The easiest way would be to explicitly enable error reporting at the top of the PHP script you wish to debug.
error_reporting(E_ALL | E_STRICT);
Alternatively, if you have access to your php.ini file and you want to always have error reporting on, change error_reporting = to a development friendly value of
error_reporting = E_ALL | E_STRICT
then change display_errors = to
display_errors = On
Finally, if access to the php.ini file is not available, you can add the following directives to your .htaccess or VirtualHost configuration for Apache:
php_value error_reporting 32767
php_value display_errors 1
If you would like a development optimized development environment like the one utilized in this course, see Up and Running with Linux for PHP Developers, here in the lynda.com online training library.
Q: This course was updated on 4/10/2013. What changed?
A: The author rerecorded some of the tutorials to add more background information and better graphics.
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.