Author: Eustaquio Rangel de Oliveira Jr.
Posted on: 2015-12-22
Package: PHPR
Read this article to learn how we can have code that is more consistent and easier to write and maintain.
Contents
Introduction
Examples of Consistency Manipulating Collections
Conclusion
Introduction
Have you ever forgot what are the parameters of a PHP class method? Or if it returns 0, true or false? The first question can be solved using IDEs, for that that like to use IDEs. I just use, and love to use, Vim with a lot of plugins. So, for me, it's not an option.
The latter question can lead to confusing behavior, because it can mess things up on some levels. We need to check, as with the first question, the documentation and perhaps the method return value, comparing it with some ways that can not be very productive.
I use to code a lot using Ruby, where we can say that most part of time it follows the principle of least surprise. You don't need to think or have any surprises with unexpected behavior of the code. And it also has a strong object oriented and functional mood with all its objects and methods, blocks, closures, whatever.
So, I'm trying to copy some Ruby features, starting with arrays (collections), to try to make some things easier in PHP. That's why I wrote the PHPR package. You can check it here. There is more detailed documentation on its wiki.
Examples of Consistency Manipulating Collections
Let's see some examples. First, create the object, passing an array (associative or not):
$t = new PHPR\Collection([0 => "zero", 1 => "one", 2 => "two"]);
Don't you just hate the "undefined index" error messages? Now is not a problem anymore,
it just returns null when the index is not found:
echo $t[10]."\n"; // => null
Of course, the array style accessor works as expected:
echo $t[1]."\n"; // => "one"
Finding collection elements that meet certain conditions is very easy:
$col = new PHPR\Collection(["one", "two", "three"]); $rst = $col->select(function($e) { return strlen($e) > 3; }); var_dump($rst->values());
array(1) { [0] => string(5) "three" }
Transforming the collection elements:
$col = new PHPR\Collection(["one", "two", "three"]); $rst = $col->map(function($e) { return strrev($e); }); var_dump($rst->values());
Outputs:
array(3) { [0] => string(3) "eno" [1] => string(3) "owt" [2] => string(5) "eerht" }
Splitting the collection in two collections, one that meets certain conditions and another that doesn't, is done using partition:
$col = new PHPR\Collection([1, 2, 3, 4, 5]); $cols = $col->partition(function($e) { return $e % 2 == 0; }); echo "even numbers:\n"; var_dump($cols[0]->values()); echo "odd numbers:\n"; var_dump($cols[1]->values());
Outputs:
even numbers: array(2) { [0] => int(2) [1] => int(4) } odd numbers: array(3) { [0] => int(1) [1] => int(3) [2] => int(5) }
$col = new PHPR\Collection([1, 2, 3, 4, 5]); echo "sum is ".$col->inject(function($memo, $value) { return $memo + $value; })."\n"; echo "sum is ".$col->inject(function($memo, $value) { return $memo + $value; }, 10)."\n";
Outputs:
sum is 15 sum is 25
Grouping the collection elements:
$col = new PHPR\Collection(["one", "two", "three"]); var_dump($col->groupBy(function($e) { return strlen($e); }));
Output:
array(2) { [3] => array(2) { [0] => string(3) "one" [1] => string(3) "two" } [5] => array(1) { [0] => string(5) "three" } }
We can even chain the methods:
$changed = self::$_col->map(function($e) { return strrev($e); })->select(function($e) { return strlen($e) <= 3; }); var_dump($changed->values());
array(2) { [0] => string(3) "eno" [1] => string(3) "owt" }
Conclusion
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
2. not sure if - Francesco (2015-12-30 18:21)
well... - 1 reply
Read the whole comment and replies
1. PHPR - Marco Barbato (2015-12-22 09:34)
missing files... - 2 replies
Read the whole comment and replies