Your question, your audience. Choose who sees your identity—and your question—with question security.
echo '<h2>Testing typecasting to an object</h2>'; $test_object = (object) array( 'hello' => 'world', 'nested' => array('key'=>'value'), ); echo '<tt><pre>'.var_export($test_object,TRUE).'</pre></tt>'; echo '<h2>Testing typecasting to an object scaler</h2>'; $test_object = (object) 12345; echo '<tt><pre>'.var_export($test_object,TRUE).'</pre></tt>';
00:00 In this chapter, I'm going to explore built-in PHP objects. 00:04 I'll start with the standard class, which is pretty generic, 00:07 then demonstrate how to get objects from the database. 00:10 We'll handle errors in an object-oriented way, then customize them to meet our needs. 00:14 Let's start with the standard class. 00:16 A standard class is a generic class that can be created by typecasting a value 00:20 as an object, like casting the string "hello, world" as an object. 00:24 It won't have any methods, but it will have values. 00:27 In case you were wondering, if you typecast an object to an object, that's kind of 00:31 goofy, and nothing will happen. 00:34 If you typecast that array to an object, the result will be an object with 00:37 properties named for the keys to the array, with values corresponding to the array values. 00:43 Let's clean up the demo, and remove the cloning tests. 00:46 Create a test standard class object out of a nested array. 00:51 Echo <h2> Testing typecasting to an object>. 01:01 $test_object = (object). This is the typecasting. array. hello as the key and 01:10 world as the value. Then nested as an array ( 'key => value'). 01:24 Then, debug the test object. 01:29 Save, then refresh your browser. 01:33 The result will be a standard class with properties "hello" and "nested," with nested 01:37 containing an array. 01:39 If I was to typecast any other data type like a string, the value would be 01:43 converted to a standard object, with one property named scalar. 01:47 Return to the demo, and replace the array definition with the number 12345. Save, and refresh. 02:00 The test object now has one property, scalar, with the value 12345. 02:07 Standard class objects are practically used in a number of ways. 02:10 For example, returning a defined data structure with fixed branches without nesting. 02:16 Sometimes, it's used as a shorthand to speed code development, as it takes less 02:20 characters to access a property than it does to specify a key. 02:24 Finally, you can use a standard object when interacting with a method that is 02:28 looking for an object with particular properties, but isn't actually 02:31 checking for the class. 02:33 In the next video, I will show you how to load objects directly from the 02:37 database, then intelligently load addresses from the database into the 02:40 correct class.