Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When developing an application, I'm typically working with many different kinds of data in different structures, each with a different behavior and need. However, if I were to use different naming conventions for every single data structure, my application would descend into chaos! The phrase "Spaghetti code" is an apt description, meaning the source code is impossibly complex and tangled, like a pile of pasta. Sounds delicious, but it's difficult to organize. To avoid this mess, I can use an object interface. An interface specifies what methods a class must implement, but doesn't say how those methods should be implemented.
There are similarities to an abstract class, but the main importance is that interfaces don't have any methods of their own. In fact, an interface is not a class at all, but it's similar enough that auto loading will work with it. To use an interface, a class uses the keyword implements, followed by the name of the interface. A class can implement more than one interface at a time by separating their names with a comma, as long as those interfaces don't have conflicting method names. Interfaces can also have constants, which will be available to any classes that implement it.
Interface constants cannot be overridden or defined. Overwriting is going to be covered in greater detail in the next segment. So, what's the point of all this? Well, the practice of sharing a common application programming interface, or API, between classes is known as polymorphism. And, interfaces fit that definition quite well, because they're designed exactly for that purpose. In our application, I'm going to define an interface that both the address class and future classes must implement. When designing an interface, try to be as generic as possible, yet still kind of specific enough to be useful.
That sounds like a hard line to walk. So, to demonstrate this, I am going to create a model. A model is responsible for managing data, and storing and retrieving entities, and containing business logic. This aptly describes what the address class is going to be doing at a high level, and applies to future classes as well. I am going to the switch to the IDE. Let's create a new file called Class.Model.Inc. Start with the keyword interface, followed by the name. Shared interface for interactions.
I am going to require two methods in this interface. The first is Load, which will load from the database, given an address ID. I am using Static, as I can't guess what type of address will be returned, so I'll add logic that will return the correct address subclass. The second method will be Save, which will just save to the database. static function load, which takes an address_id. Load a model. param int $address_id. Next, function save, which will save a model. Save, then switch to the Address class.
You want to make the Address class implement the new model interface. So, after the name of the class, add the following: implements Model. If I were to save and refresh the demo, I'd get a fatal error, because the address class doesn't declare the methods from the model interface. We are setting up a situation where there's the possibility that a childs class can potentially declare a method that's already in the parent, like Load or Save. This is known as an override. I don't want to let that happen.
So, I'm going to use the keyword Final. When used on a method, Final prevents childs classes from overwriting the method. So, only the parent address class can describe the save behavior. I can also declare a class as Final, which will keep it from being extended. I'm going to make the Load and Save methods in the address class Final. At the end of the address class, add a final public static function load, which accepts an address_id, placeholder, followed by a final public function save.
Add documentation. param as an integer. And, add documentation. save the address. For now, I'm going to leave the declarations empty, and leave them for a later segment. With the introduction of the final keyword, I mentioned overrides, but didn't go into much detail. In the next segment, I'll discuss how to override methods, properties, and constants.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97255 Viewers
61 Video lessons · 84534 Viewers
71 Video lessons · 68734 Viewers
56 Video lessons · 101220 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.