underscore.php

underscore是前端經常使用的javascript庫,最近無心中發現原來它還有PHP的移植javascript

基本介紹

絕大部分函數都移植過來了。underscore.js用的是_,但是這個在PHP中已經用來表示gettext()了,因此改爲了__,雙下劃線。php

使用很簡單:前端

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

或者,若是你偏心靜態風格:java

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

結果:git

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

安裝

composer

通常用Composer安裝:github

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

Laravel

若是用Laravel框架,能夠用artisancomposer

artisan bundle:install underscore

而後在bundles文件中加入這行:框架

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

include/require

若是你這麼傳統……那固然也是能夠的。ide

函數一覽

絕大部分都移植過來了:函數

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。不過這個庫能夠幫助從前端轉過來的同窗,因此這裏和你們分享一下。

相關文章
相關標籤/搜索