Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Often when working with interfaces, you will be using existing interfaces such as the built-in Iterator interface. By combining multiple interfaces we can build powerful components that can be easily understood by the rest of our application.
[MUSIC]
0:00
Now that we have some data to work with,
we want to create a collection that we can
0:05
use for loops and
perform some additional functionalities.
0:09
For the looping capabilities,
PHP offers us an iterator interface, so
0:13
we don't need to create our own.
0:18
Often, when working with interfaces,
0:22
you will use existing interfaces
such as the iterator.
0:23
Besides the interfaces
that are built into PHP,
0:28
most frameworks include
their own interfaces.
0:32
These interfaces work as a kind
of guaranteed documentation for
0:35
using individual components.
0:39
By combining multiple interfaces,
we can build powerful components
0:41
that can be easily understood
by the rest of our application.
0:46
Let's go back into our collection file.
0:50
We are going to use implements Iterator.
0:52
This implements the built
in iterator interface.
0:57
This implements keyword is
how we specify that our class
1:01
will meet the contract
of a given interface.
1:06
We add the name of the interface and
1:09
then it's up to us to implement
the details of that interface's methods.
1:11
Somewhere in this class, I mentioned
that we can combine interfaces together
1:16
to give our class more and
more capabilities.
1:20
We can do that by adding more
interface names separated by comma.
1:24
So for this example, we also want
the collection to be countable, so
1:28
we can add a comma and
then the built-in Countable.
1:33
We'll use two properties,
1:36
protected $repo; and
a public $collection;.
1:39
Then we'll set up a construct.
1:48
On construct,
we're going to pass the $repo and
1:50
optionally the ID and
field if we want to limit our collection.
1:53
We don't care where the data is coming
from, but we do need to know that we can
1:58
access the find and all methods that
we set up in our repository interface.
2:02
Type declarations,
also called type hinting,
2:08
allows us to require a specific
type of parameter on a method call.
2:11
Type hinting for classes and
2:16
interfaces was added in PHP5
along with arrays and callable.
2:18
PHP7 added scaler type hints.
2:22
These are the basic variable types of
Boolean, float, integer and string.
2:25
To specify that our first parameter must
2:30
implement the repository interface,
2:35
we specify the name of
the interface before the parameter,
2:40
public function __construct(
2:47
RepositoryInterface, and then $repo,
2:51
then we can add our optional parameters,
2:56
$id = null, $field = 'id').
3:01
On construct,
we first set the local $repo,
3:06
then we populate our collection $this ->
repo = $repo; if the ID is not empty.
3:11
Then we're going to say $this ->
3:23
collection = $this->repo-> find(.
3:28
We're going to past posts,
3:35
the ID and the field.
3:40
Else.
3:44
$this->collection = $this->
3:47
repo->all('posts');.
3:54
Now we need to implement the methods from
the iterator and countable interfaces.
4:00
You need to sign up for Treehouse in order to download course files.
Sign up