Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So far, I've created an address class, given it a number of logical properties for storing data, and declared a display method. It's time to put it to good use by creating an instance of the address class. This process is referred to as instantiation, and the result is an object of the instantiated class. To create an instance of a class, start with a variable declaration. Then use the new keyword followed by the name of the class that you wish to instantiate. Let's create a new file named demo.php, and place it in the same folder as your address class.
At the top of the file, require the address class.
Next, create a new variable named address assigned to the new address.
Add the following lines:
Instantiating Address, $address = new Address.
For debugging purposes, you can inspect the contents of the entire object using
var dump and var export.
Add the following lines:
Empty Address, echo
and close , save, then open your web browser, and navigate to the
location of your exercise files.
var_export $address, followed by TRUE,
You should see the object of class address displayed with all properties set to null. As you may recall, you can access the properties of an object using the object property access syntax, '->'. You can both get and set these properties. Set each of the properties of the address, then debug the result. Go back to the demo source: echo, setting properties, $address-> street_address_1 = '555 Fake Street', $address->city_name = 'Townsville', $address->subdivision_name is State, $address->postal_code = '12345', $address-> country_name is the United States of America.
We'll debug the object at the end using var export. Save. Now, let's switch back to the code, and refresh your browser to see the populated address. In addition to properties, you can also access an object's class method using the same syntax. Display the address in the demo file. Now, let's switch back to the code: echo displaying address, and echo address and the display method, save, and refresh.
You've now instantiated your first object, manipulated its properties, debugged its contents, and executed a method. In the next segment, I'm going to explore how access can be controlled to properties and methods using visibility.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97456 Viewers
61 Video lessons · 84687 Viewers
71 Video lessons · 68887 Viewers
56 Video lessons · 101331 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.