Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed PHP Standards and Best Practices!
You have completed PHP Standards and Best Practices!
Preview
Now that we know how Composer works, we can look into how packages are distributed and how you can package up your code to be used by other developers.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
So far in this course we've been building
really simple applications.
0:04
They all have an index.php and run in the
browser.
0:08
A component, sometimes called a package
interchangeably,
0:10
does not run in the browser, but is used
by an application or another component.
0:13
That means we need to approach them
slightly differently, but
0:17
they're still quite similar.
0:19
Let's have a go at making a really basic
component with a simple structure.
0:21
Then we can look at how we write classes
and
0:24
how we check to see if they work when we
build them.
0:26
First, we'll need an SRC folder, which is
where all of our code will live.
0:29
Inside there we're going to create a
single class called Example.php.
0:35
This is a drastically simplified version
of a component, and
0:40
your component will probably have a lot
more classes to it.
0:42
But, for the sake of simplicity, we're
just gonna have the one.
0:45
Let's throw some code in here.
0:48
And save that out.
0:49
Here we're putting our code inside a
namespace, Treehouse/Example.
0:51
And inside here we're defining a class
also called Example.
0:56
This repetition is quite common.
1:01
Many packages will have one main class to
interact with.
1:02
And this can be the same name as the
component itself.
1:05
Finally on line seven we're defining a
method named getSomething.
1:08
And we're just returning a static string
for the sake of simplicity at this point.
1:12
So far we've been checking our code works
by running it in the browser.
1:17
Unlike with an application we don't have
an index.php for this component.
1:20
And to be honest we don't want or need
one.
1:24
It would be much more useful if we used
unit tests instead of hacking together
1:26
some browser code and manually checking
things work okay.
1:29
Unit testing is a huge subject which is
covered well by other courses.
1:32
But we can certainly cover the basics
here.
1:35
First let's install php unit as a dev
dependency.
1:37
Once again, we can use composer require
phpunit,
1:40
which is the name of the vendor, and the
name of the package itself.
1:44
And then because this is a dev dependency,
remember we don't wanna have to
1:49
force people to install phpunit on their
production service.
1:52
We're gonna run --dev and the version
constraint is 4.2.wild card because I
1:55
know that 4.2, at the moment, is the
latest version.
2:00
So there we can see it's created a
composer.json for us and
2:04
it's installed all of php units
dependencies itself and php unit 4.2.5.
2:08
Perfect.
2:13
At this point we have the very early
stages of a composer package.
2:15
If we're going to think about releasing
this code,
2:18
there are a few other things we should
think about first.
2:20
Next, let's look at some standards to
improve the quality of our code.
2:23
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up