1 00:00:00,000 --> 00:00:04,983 [MUSIC] 2 00:00:04,983 --> 00:00:08,120 So far in this course we've been building really simple applications. 3 00:00:08,120 --> 00:00:10,930 They all have an index.php and run in the browser. 4 00:00:10,930 --> 00:00:13,820 A component, sometimes called a package interchangeably, 5 00:00:13,820 --> 00:00:17,630 does not run in the browser, but is used by an application or another component. 6 00:00:17,630 --> 00:00:19,820 That means we need to approach them slightly differently, but 7 00:00:19,820 --> 00:00:20,690 they're still quite similar. 8 00:00:21,820 --> 00:00:24,860 Let's have a go at making a really basic component with a simple structure. 9 00:00:24,860 --> 00:00:26,400 Then we can look at how we write classes and 10 00:00:26,400 --> 00:00:28,310 how we check to see if they work when we build them. 11 00:00:29,950 --> 00:00:33,260 First, we'll need an SRC folder, which is where all of our code will live. 12 00:00:35,270 --> 00:00:40,211 Inside there we're going to create a single class called Example.php. 13 00:00:40,211 --> 00:00:42,671 This is a drastically simplified version of a component, and 14 00:00:42,671 --> 00:00:45,450 your component will probably have a lot more classes to it. 15 00:00:45,450 --> 00:00:48,390 But, for the sake of simplicity, we're just gonna have the one. 16 00:00:48,390 --> 00:00:49,763 Let's throw some code in here. 17 00:00:49,763 --> 00:00:50,670 And save that out. 18 00:00:51,760 --> 00:00:56,620 Here we're putting our code inside a namespace, Treehouse/Example. 19 00:00:56,620 --> 00:01:01,120 And inside here we're defining a class also called Example. 20 00:01:01,120 --> 00:01:02,630 This repetition is quite common. 21 00:01:02,630 --> 00:01:05,550 Many packages will have one main class to interact with. 22 00:01:05,550 --> 00:01:08,000 And this can be the same name as the component itself. 23 00:01:08,000 --> 00:01:12,400 Finally on line seven we're defining a method named getSomething. 24 00:01:12,400 --> 00:01:17,210 And we're just returning a static string for the sake of simplicity at this point. 25 00:01:17,210 --> 00:01:20,450 So far we've been checking our code works by running it in the browser. 26 00:01:20,450 --> 00:01:24,320 Unlike with an application we don't have an index.php for this component. 27 00:01:24,320 --> 00:01:26,266 And to be honest we don't want or need one. 28 00:01:26,266 --> 00:01:29,479 It would be much more useful if we used unit tests instead of hacking together 29 00:01:29,479 --> 00:01:32,480 some browser code and manually checking things work okay. 30 00:01:32,480 --> 00:01:35,660 Unit testing is a huge subject which is covered well by other courses. 31 00:01:35,660 --> 00:01:37,600 But we can certainly cover the basics here. 32 00:01:37,600 --> 00:01:40,920 First let's install php unit as a dev dependency. 33 00:01:40,920 --> 00:01:44,600 Once again, we can use composer require phpunit, 34 00:01:44,600 --> 00:01:49,487 which is the name of the vendor, and the name of the package itself. 35 00:01:49,487 --> 00:01:52,463 And then because this is a dev dependency, remember we don't wanna have to 36 00:01:52,463 --> 00:01:55,410 force people to install phpunit on their production service. 37 00:01:55,410 --> 00:02:00,220 We're gonna run --dev and the version constraint is 4.2.wild card because I 38 00:02:00,220 --> 00:02:03,580 know that 4.2, at the moment, is the latest version. 39 00:02:04,800 --> 00:02:08,485 So there we can see it's created a composer.json for us and 40 00:02:08,485 --> 00:02:13,435 it's installed all of php units dependencies itself and php unit 4.2.5. 41 00:02:13,435 --> 00:02:15,358 Perfect. 42 00:02:15,358 --> 00:02:18,330 At this point we have the very early stages of a composer package. 43 00:02:18,330 --> 00:02:20,320 If we're going to think about releasing this code, 44 00:02:20,320 --> 00:02:23,020 there are a few other things we should think about first. 45 00:02:23,020 --> 00:02:25,740 Next, let's look at some standards to improve the quality of our code.