Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There's a strong possibility that you're already familiar with constants. They are common place in procedural code. As a natural extension, it's possible to add a constant to a class, which can be good for code organization. A class constant is like a property with a value that never changes. Constants are useful for defining things like error codes, data structure names, and values that have no need to change at runtime, if ever. By associating a constant with a class, a developer can add context rather than having to resort to long, complicated names.
While constants use the same PHP label naming convention, best practice dictates that constant names are all in capital letters separated by underscores. Constants can only contain simple values, so strings, booleans and integers will work, but an array will not. To define a constant within a class, start with the keyword "const," followed by the name with no dollar sign ($), an assignment operator, and a value. Let's said add constant to the address class to store the address type IDs.
I'm going to open the Address class, and navigate to the top of the file, then add three constants for each of the address type values. const ADDRESS_TYPE_RESIDENCE = 1, const ADDRESS_TYPE_BUSSINESS = 2, and then const ADDRESS_TYPE_PARK = 3. Part of the reason why I did not specify that the address types be defined as a constant is that arrays are not allowed as constant values. With that said, you can replace the hardcoded values with the constants that were just declared.
Similar to static properties, you use the scope resolution operator to access constants, only without the dollar sign ($). Edit the static address types, and replace the keys with the constant values. Address::ADDRESS_TYPE_RESIDENCE, Address::ADDRESS_TYPE_BUSSINESS and Address::ADDRESS_TYPE_PARK and save. There is no functional difference between having the raw numbers there in the constants, which is part of the goal.
The advantage is that there's only one place where those values are set, which makes them available to any other code. I can refer it to these address type IDs with semantic human readable names, as opposed to trying to have to remember what number is associated with them. In a moment, I'll demonstrate how to write a static method for validating address type IDs using these class constants. From a code maintainability perspective, those constant values are now available to any other piece of code, and can be referenced using semantic names, as opposed to trying to remember what number is associated with which value.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101631 Viewers
61 Video lessons · 88386 Viewers
71 Video lessons · 72224 Viewers
56 Video lessons · 103973 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.