underscore.php

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了underscore.php脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

underscore是前端常用的javascript库,最近无意中发现原来它还有PHP的移植

基本介绍

绝大部分函数都移植过来了。underscore.js用的是_,可是这个在PHP中已经用来表示gettext()了,所以改成了__,双下划线。

使用很简单

__(array(1, 2, 3))->;map(function($n) { return $n * 3; });

或者,如果你偏爱静态风格:

__::map(array(1, 2, 3), function($n) { return $n * 3; });

结果:

Array
(
    [0] => 3
    [1] => 6
    [2] => 9
)

安装

composer

一般用Composer安装:

"anahkiasen/underscore-php" : "dev-master"

Laravel

如果用Laravel框架,可以用artisan

artisan bundle:install underscore

然后在bundles文件中加入这行:

'underscore' => array('auto' => true),

include/require

如果你这么传统……那当然也是可以的。

函数一览

绝大部分都移植过来了:

Collections

each, map, reduce, reduceRight, detect, select, reject, all, any, includ, invoke, pluck, max, min, groupBy, sortBy, sortedIndex, toArray, size

Arrays

First, rest, last, compact, flatten, without, uniq, union, intersection, difference, zip, indexOf, lastIndexOf, range

Functions

memoize, throTTLe, once, after, wrap, compose

Objects

keys, values, functions, extend, defaults, clon, tap, isEqual, iSEMpty, isObject, isArray, isFunction, isString, isNumber, isBoolean, isDate, isNaN, isNull

Utility

identity, times, mixin, uniqueId, template

Chaining

chain, value

我自己更习惯写原生PHP。不过这个库可以帮助从前端转过来的同学,所以这里和大家分享一下。

脚本宝典总结

以上是脚本宝典为你收集整理的underscore.php全部内容,希望文章能够帮你解决underscore.php所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。