In a work project, we are trying to use libphutil in the same project as illuminate/database (the eloquent database driver from Laravel). The illuminate/database pulls in illuminate/support and defines the head() and last() functions [[https://github.com/illuminate/support/blob/master/helpers.php#L474|as seen here]], which causes the following error when loading the libphutil classes:
Fatal error: Cannot redeclare head() (previously declared in /var/www/proj/vendor/illuminate/support/helpers.php:482) in /var/www/proj/vendor/phacility/libphutil/src/utils/utils.php on line 679
This patch adds function_exists() guards to libphutil, in order for the two libraries to play nice together.
```
diff --git a/src/utils/utils.php b/src/utils/utils.php
index 136a354..8c9142c 100644
--- a/src/utils/utils.php
+++ b/src/utils/utils.php
@@ -674,8 +674,10 @@ function newv($class_name, array $argv) {
* @param array Array to retrieve the first element from.
* @return wild The first value of the array.
*/
-function head(array $arr) {
- return reset($arr);
+if (!function_exists('head')) {
+ function head(array $arr) {
+ return reset($arr);
+ }
}
/**
@@ -686,8 +688,10 @@ function head(array $arr) {
* @param array Array to retrieve the last element from.
* @return wild The last value of the array.
*/
-function last(array $arr) {
- return end($arr);
+if (!function_exists('last')) {
+ function last(array $arr) {
+ return end($arr);
+ }
}
```