Implementing an Interface8:07 with Alena Holligan
An interface defines the public facing interaction of an item. It does not define any implementation details about the inner workings of those interactions. By implementing an interface, we agree to implement the methods described in the contract of the interface.
Course: File Handling with PHP - JSON
The decoding in PHP 5 is handled by a parser based on the JSON_checker by Douglas Crockford. PHP 7 comes with a completely new and improved parser that is specifically written for PHP and licensed under the PHP license. Either version uses the following functions:
json_encode - Returns a string containing the JSON representation of the supplied value.
json_decode - Takes a JSON encoded string and converts it into a PHP variable.
It does not define any implementation details 0:00 about the inner workings of those interactions. 0:05 The engine of an electric car may function very differently than the engine of 0:10 a gas powered car, but both vehicles have a pedal for accelerating. 0:15 When working with data, the way we store, retrieve, parse and decode that data 0:21 may change significantly, dependent upon the type of data being used. 0:27 To demonstrate these differences, I've provided two types of data for 0:32 this project, a SQLite file, and a JSON file. 0:37 Even though retrieving data from these sources is very different, 0:42 we want to interact with them in the same way. 0:46 We want to retrieve [SOUND] all items from a particular table or entity. 0:49 [SOUND] And we also want to be able to find a single item. 0:53 We've already written the interface that defines these requirements. 0:57 Now we're ready to build our classes, 1:02 which will implement the repository interface. 1:04 Let's start with the JSON data. 1:08 If you're not familiar with JSON, or working with JSON in PHP, 1:10 check the notes associated with this video. 1:13 In the src > classes folder, create a new file, 1:17 Named jsonRepository. 1:24 Open php, and add class jsonRepository. 1:31 Then we use the keyword implement, And the RepositoryInterface. 1:40 For JSON data, the only requirement is the JSON file to which we wish to connect. 1:54 Let's add a property, and 2:00 set that on construct. 2:04 Protected $file, public function __construct($file). 2:08 $this -> file = $file. 2:21 Let's take a look at what happens when we try to instantiate this class now. 2:28 Let's go under src > config. 2:32 $repo = new JsonRepository, 2:38 And we'll pass the file. 2:46 We'll use the current directory, And then /database.json. 2:48 Now let's preview the site. 2:56 We're given an error that tells us the information we need to know. 3:00 The jsonRepository class that we tried to implement contains two abstract methods. 3:03 We have two choices for solving this error. 3:10 The first option is to declare the class as abstract. 3:12 We'll cover abstract classes a little later, but for now, what we need to know 3:16 is that if it's abstract, we cannot instantiate that class. 3:21 So that's not the option we want. 3:26 Instead, we should implement the remaining methods. 3:28 The error also tells us what these methods are, all and find. 3:32 Let's quickly create these methods, and try the preview again. 3:37 Public function all, And public function find. 3:46 Now when we preview the site, 3:58 we're given another error telling us that our all method must be compatible 4:00 with the all method declared in the repository interface. 4:05 This requires us to accept a single parameter. 4:09 We do not need to use the same parameter names as the interface, but 4:13 we do need to use the same number of parameters. 4:17 Also, if a parameter in a given position is declared as optional in the interface, 4:22 the parameter in the same position must be declared as optional in the method. 4:28 You can add a default value to any parameter, and 4:34 even add an additional parameter, as long as it is optional and has a default value. 4:37 Let's finish out these methods. 4:43 We'll need to load the entire JSON file for either option, so 4:48 let's start with the all method. 4:50 We need to accept an entity, 4:55 And then for our data, we need to 5:01 json_decode(file_get_contents($this->fil- e)). 5:06 Then we're going to return $data, And the passed entity. 5:16 Now for the find method. 5:25 We need to accept the entity, The value to find, 5:28 and optionally, the field to search against. 5:33 We'll set the default to id. 5:38 For JSON files we'll need to load the entire file, so 5:42 we can link to our all method for that. 5:45 We'll need to loop through the items from our JSON file. 5:48 Foreach($this->all, and 5:50 pass the entity, 5:57 as $key=>$data. 6:01 Then we'll check if ($data->$field == $value). 6:11 Then we're going to return array($data). 6:23 We're going to return this as an array so 6:29 that it matches the results of our all method. 6:31 This function is only going to return a single item that matches the request. 6:34 This is how we want the find method to work. 6:39 If we want to create a search method that returns more than one item, 6:42 we could use the same pattern. 6:46 Except we would not want to return until we had found 6:48 all items that match the search. 6:52 Now let's preview the site again. 6:54 No more errors, great. 6:59 Let's try to retrieve all posts. 7:01 Let's var_dump($repo->all('posts')). 7:07 Looks good. 7:26 Let's try to find a single post. 7:29 Instead of all we use find, pass posts, and then 1. 7:36 And we only get the single first post. 7:46 Great, now I'll give you a chance to implement the repository interface 7:54 to connect to the SQLite database. 7:58 Remember, the methods will remain the same, 8:01 but the way the data is retrieved will change. 8:03
You need to sign up for Treehouse in order to download course files.Sign up