Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As you may recall, a class property is a structure for storing values. A class can have any number of properties associated with it. Each property is defined with a visibility keyword: public, protected, or private. If you leave out the keyword, you're going to get a parse error. I'll explore visibility scope in greater detail in a later video. For the time being, let's just stick with public properties that have no access restrictions. Properties are declared kind of like regular variables, including the naming conventions for PHP labels. Same as class naming, property names must either start with a letter or underscore, then be followed by any number of letters, numbers, and underscores.
When naming a property, a best practice is to use lowerCamel naming, meaning the first letter of the name needs to be lowercase, and then the first letter of any remaining words should be capitalized. Avoid naming a public property with an underscore at the beginning. Depending on the coding standard, this may visually indicate that it's actually private access only. When you're declaring a property, you can optionally initialize it with the default value. If you do, it must be an unchanging value. This means that it must be able to be evaluated upon compilation. So, things that change, like the current time, are not allowed.
Let's clean up all the test code, then build upon the first address class. public $streetName. This is a valid property name that follows best practices. public $subdivision_name = California. This is also a valid property name that includes an initialization. public $_city. This is a valid property name, but as described before, does not follow best practices by implying that it has a different variable scope.
public $time_updated = time. This is a valid property name, but as the value is determined at runtime, this is an illegal initialization, and will generate a parse error. public $-addressId. This is an invalid property name, as it starts with a dash, not an underscore. This will generate a parse error, expecting a T variable. public $city_copy = $_city. This is an invalid initialization, as it depends on a runtime evaluation.
So, it will also trigger a parse error. Let's clean up this example, and make it useful in a real world application, and add some documentation. For the time being, I'll use human readable data for properties with known domains, such as the city, subdivision, and country name. We'll start with basic class documentation. I will add the street address properties. public $street_address_1, public $street_address_2.
Following the address, let's add the structure for the name of the city. public $city_name. After the city, I'll add the name of the subdivision. This can be a state, territory, region, and so forth. public $subdivision_name. Most addresses have a postal code of some sort. public $postal_code. And finally, the name of the country. public $country_name.
Now that we have a class definition, let's save it before we move on to creating a method in the next video.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96710 Viewers
56 Video lessons · 110427 Viewers
71 Video lessons · 79251 Viewers
131 Video lessons · 38005 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.