Writing an Interface3:57 with Alena Holligan
Interfaces allow us to talk about requirements without the need for implementation details. In other words, what do we need to be able to do with this object? This will help with both building a class and implementing an object.
Documentation for Object Interfaces
Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
Interfaces are defined in the same way as a class, but with the interface keyword replacing the class keyword and without any of the methods having their contents defined.
All methods declared in an interface must be public; this is the nature of an interface.
To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.
Constants: It's possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits them.
Before we can fill in our collection class, 0:00 we need to connect to a data source. 0:02 There are many different data sources we could use, including databases, 0:05 files, and feeds. 0:10 One thing that interfaces allow us to do is talk about requirements 0:12 without the need for implementation details. 0:17 In other words, what do we need to be able to do with this object? 0:19 This is useful for both building a class and implementing an object. 0:24 For this application, we'll be retrieving data in two ways. 0:29 We need to be able to retrieve all items from a certain table or entity. 0:33 We also need to be able to find a single item. 0:38 Let's create our first interface that defines this contract. 0:43 In the source directory, 0:47 we're going to organize things a little by creating a new folder named interfaces. 0:49 We want this to be a generic store of data. 0:57 So I'll be using the term repository instead of database. 1:01 It is common practice to name an interface by appending the word interface. 1:05 Let's create a new file. 1:10 Named RepositoryInterface.php. 1:15 Interfaces are defined in the same way as a class, 1:27 using the interface keyword instead of the class keyword. 1:30 To create the contract, we define which methods must be implemented. 1:44 But we do not define the contents of these methods. 1:49 The contract assures us that these methods will be available to access. 1:54 Therefore, all methods declared in an interface must be public. 1:59 Let's define the required method. 2:04 First, we want to retrieve all items of a certain table or entity. 2:06 So we use public function all and then accept the entity. 2:12 Because we are not defining the contents of this method, 2:20 we do not use curly braces. 2:24 Instead, we end the line with a semicolon. 2:26 Next, we want to find a single item. 2:30 So we use public function find. 2:33 This will again accept the entity, but 2:38 also the identifier, and optionally, the field name. 2:42 We'll default this to id. 2:49 Okay, so now our new interface has the two methods we care about. 2:54 Again, this is the contract that anything must follow if 2:59 it wants to use this interface. 3:03 This is really the key idea of interfaces in all programming languages. 3:06 You define what something must do, and then whatever class is going to perform 3:11 those tasks must provide their own details for how that task will be accomplished. 3:16 Because we are defining what, and not how, we are not defining 3:23 the state of an object, and therefore cannot define properties in an interface. 3:28 You can define constants because they do not change state. 3:34 These constants may not be overridden by other interfaces or implementing classes. 3:39 Check the notes associated with this video for more details. 3:44 Now that we have defined our interface, we're ready to implement this interface 3:48 to create a connection to the data source we wish to use. 3:53
You need to sign up for Treehouse in order to download course files.Sign up