Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this chapter, I'm going to explore built-in PHP objects. I'll start with the standard class, which is pretty generic, then demonstrate how to get objects from the database. We'll handle errors in an object-oriented way, then customize them to meet our needs. Let's start with the standard class. A standard class is a generic class that can be created by typecasting a value as an object, like casting the string "hello, world" as an object. It won't have any methods, but it will have values. In case you were wondering, if you typecast an object to an object, that's kind of goofy, and nothing will happen.
If you typecast that array to an object, the result will be an object with
properties named for the keys to the array, with values corresponding to the array values.
Let's clean up the demo, and remove the cloning tests.
Create a test standard class object out of a nested array.
Testing typecasting to an object>.
$test_object = (object). This is the typecasting. array. hello as the key and
world as the value. Then nested as an array ( 'key => value').
Then, debug the test object. Save, then refresh your browser. The result will be a standard class with properties "hello" and "nested," with nested containing an array. If I was to typecast any other data type like a string, the value would be converted to a standard object, with one property named scalar. Return to the demo, and replace the array definition with the number 12345. Save, and refresh.
The test object now has one property, scalar, with the value 12345. Standard class objects are practically used in a number of ways. For example, returning a defined data structure with fixed branches without nesting. Sometimes, it's used as a shorthand to speed code development, as it takes less characters to access a property than it does to specify a key. Finally, you can use a standard object when interacting with a method that is looking for an object with particular properties, but isn't actually checking for the class.
In the next video, I will show you how to load objects directly from the database, then intelligently load addresses from the database into the correct class.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97118 Viewers
61 Video lessons · 84417 Viewers
71 Video lessons · 68626 Viewers
56 Video lessons · 101113 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.
Your file was successfully uploaded.