forAll, partition, slice, and more

In this final video in looking at Doctrine's Collection interface, and more specifically, the ArrayCollection implementation of the Collection interface, we cover the following remaining methods:

  • forAll()
  • partition()
  • get()
  • indexOf()
  • slice()

In the previous video we covered methods such as map and filter which take a Closure as the functions argument, and we looked at why although quite scary sounding, when you understand the underlying concept, there is nothing to be afraid of when creating our own Closure (or functions).

If you are unsure about any of this, I advise you to check out the previous video and the associated show notes where I go into more detail about this concept.

Layman's Guide to Predicate

If there's one thing that frustrates me about software development it's the reliance on computer science textbook speak. If you have a formal comp. sci. background then cool beans, but if you don't this is exactly the sort of techno-babble that can hamper your learning experience, especially if you don't have someone more experienced readily available to ask.

Ok - so there are obvious reasons to using the correct terminology, but telling a beginner that he / she needs to apply a given predicate to every element in our Collection is making things more complicated than needs be.

Simply - a predicate is a function that returns true or false. Easy as that.

We, as developers, define our own function that returns that boolean value.

A quick example would be to imagine we had an array of numbers, 1 to 10.

Our condition (predicate) is that all numbers must be less than 20. Therefore our function could be:

private function isLessThan20($value) {
    return (int) $value < 20;

We pass in a value, PHP makes sure our $value is an integer (the (int) bit), and then checks that the $value is less than 20. If yes, return true, if no, return false.

Your conditional / function can be more or less complex than this, but the premise is the same - do some logic and return either true or false.

For One forAll

We saw in the previous video how filter also takes a predicate, and that we get back an array containing those elements that matched our filtering function.

In the case of forAll however, all of our elements must match the conditional / predicate. If all our elements match the condition then we get back a value of true, otherwise the outcome of our forAll would return false.

In my opinion, the hard part of understanding the more advanced methods of Doctrine's Collection interface is figuring out the requirements of the method signatures (i.e. wtf is meant by Closure $p) rather than actually using them.

And, now that you have seen how each can be used, hopefully you will start seeing many places they can be used to good effect.

Remember, any time you are unsure about how these functions are used, don't be afraid to experiment using tools like PHPUnit or a PHPSpec test. That way you can validate your assumptions, ensure your code works as expected, and maintain a high level of code quality.


# Title Duration
1 The Basics of A Doctrine Collection 06:04
2 Frequently Used ArrayCollection Methods 06:58
3 Filter, Map, and Exists 06:11
4 forAll, partition, slice, and more 06:38