PHPackages                             rodnaph/singer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rodnaph/singer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rodnaph/singer
==============

PHP Threading

0.6.2(10y ago)647.8k2[1 PRs](https://github.com/rodnaph/singer/pulls)PHPPHP &gt;=5.3.3

Since Aug 11Pushed 10y ago1 watchersCompare

[ Source](https://github.com/rodnaph/singer)[ Packagist](https://packagist.org/packages/rodnaph/singer)[ RSS](/packages/rodnaph-singer/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (1)Versions (19)Used By (0)

Singer, PHP Threading
=====================

[](#singer-php-threading)

PHP provides lots of utilities, but calling them in an understandable way usually leads to a mess of code. Singer tries to clean this up by allowing 'threading' of results through functions/methods, giving you a readable chain of business logic.

Threading
---------

[](#threading)

By 'threading' I don't mean operating system threads, I mean the threading of the result of one function/method into the next. With PHP we usually need to either assign the results of function calls to temporary variables (to pass them in to the next function)…

```
$values = array(1, 2, 3);
$evenValues = array_filter($values, $even);
$incdValues = array_map($inc, $evenValues);
```

Or we nest our function calls so the results go straight into the next…

```
$result = array_map(
	$inc,
    array_filter(
        $values,
        $even
    )
);
```

This is a very simple example, the more steps your have in your data processing the worse it gets.

Singer Usage
------------

[](#singer-usage)

Taking the above simple example, this is how you can rewrite it using Singer.

```
use Singer\Thread as T;

// standard version

$values = array(1, 2, 3);
$evenValues = array_filter($values, $even);
$incdValues = array_map($inc, $evenValues);

// singer version

T::singer($values)
    ->filter($even)
    ->map($inc)
    ->value();
```

So, Singer allows stitching together of functions, so our code doesn't need to be concerned with handling temporary variables to control the flow of the data. Instead of following variable assignments and tracking them being fed back into other functions, the flow of processing is much more evident.

Thread First/Last
-----------------

[](#thread-firstlast)

There are a few different methods of threading that Singer allows. The default is called 'thread last' - which means that the result of each function is passed as the *last* argument to the next function.

The second way you can thread data is called 'thread first' - and means that the result of each function is passed as the *first* parameter to the next in the chain.

You can switch between these using *threadFirst* and *threadLast* methods, as shown below.

```
function array_last($array)
{
    return $array[count($array) - 1];
}

T::create(array(1,2,3))
    ->threadLast()
    ->array_last()
    ->threadFirst()
    ->range(10)
    ->value(); // array(3,4,5,6,7,8,9,10);
```

In this example we're calling our own *array\_last* function, and also the core PHP function *range*.

Thread last is meant to operate on functions which take an *array* ("sequence" functions like *array\_map*), while thread first is meant to work with functions that operate on a single value (eg. *str\_to\_upper*).

Thread to nth
-------------

[](#thread-to-nth)

You can also thread into arbitrary positions with *threadNth*

```
T::create($x)
    ->threadNth(3)
    ->someFunc($one, $two)
    ->value();
```

This uses 1-based indexing. So *1* for the first argument position, *2* for the second, etc...

Namespaces
----------

[](#namespaces)

When calling functions on the threader by default it'll look for those functions in the root namespace. Which is fine for the standard library functions. To change the namespace, use the *inNamespace* method.

```
T::create('foo bar baz')
    ->threadFirst()
    ->inNamespace('String\Utils')
    ->sentenceCase()
    ->inNamespace('Word\Utils')
    ->countWords()
    ->value(); // 3
```

Again, you can change the namespace scope arbitrarily.

Static/Object Methods
---------------------

[](#staticobject-methods)

Until now we've just been calling plain old functions, but you can also call class and instance methods too.

```
T::create('foo')
    ->onClass('Some\Static\Class')
    ->the_method() // Class::the_method()
    ->onObject($foo)
    ->bar() // $foo->bar()
    ->value();
```

And as usual, these can be included as needed during execution.

Two constructors
----------------

[](#two-constructors)

There are two constructors for creating a Singer object, the first is *create*

```
T::create($initial);
```

Which will create a threading object, using *thread last*, in the root namespace. You can use this right away to call all the PHP standard library functions.

The second constructor is *singer*

```
T::singer($initial);
```

Which creates a threading object using *thread last*, in the [utilities namespace](docs/util.md). This namespace provides a bunch of function which are meant to provide a cleaner and more consistent API than PHP's.

```
T::singer(array(1,2,3))
    ->map($inc)
    ->filter($even)
    ->reduce($toTotal, 0)
    ->value();
```

Both these methods create the same threader object, just with different default namespaces. You can switch them about with all the options above.

Debugging...
------------

[](#debugging)

It can be useful to check the value of the context that is currently being processed at different points. To do this you can use the *debug* function.

```
T::create(array(1, 2, 3))
    ->map($inc)
    ->debug() // will print_r(array(2, 3, 4)) and exit
    ->filter($odd)
    ->value();
```

If you don't want to use the default *print\_r* then you can pass your own function which will be given the context.

```
T::create(array(1, 2, 3))
    ->map($inc)
    ->debug(function($context) { ... });
```

Isn't this like...
------------------

[](#isnt-this-like)

Chaining? No, chaining is a technique of calling multiple methods on the same object.

Undercore.js chaining? More like this yeah, but with the control to thread first/last and nth.

What doesn't Singer do?
-----------------------

[](#what-doesnt-singer-do)

Singer works best when you're using a more functional approach to writing PHP, and if you use the functions in the utilities namespace (I think) it works quite nicely.

While it does support calling method on objects, if you do find yourself doing this a lot then maybe you should think if it's really the most understandable thing (as your logic is probably to spread across disparate objects... or something...).

Installation
------------

[](#installation)

To install Singer you can either just clone/download from Github, or install [through Composer](https://packagist.org/packages/rodnaph/singer).

About
-----

[](#about)

The idea behind Singer is to try to encourage separation of functionality into the most isolated parts possible. Then allowing you to easily pull these bits together at the last possible moment.

If you'd like to contribute ideas or code just open an issue or pull request.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~62 days

Recently: every ~243 days

Total

17

Last Release

3701d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e923c75f23aa7ad12c1d36e6458739d3ed606eb65a481ee26d7b0ac3f9408f17?d=identicon)[rodnaph](/maintainers/rodnaph)

---

Top Contributors

[![rodnaph](https://avatars.githubusercontent.com/u/447579?v=4)](https://github.com/rodnaph "rodnaph (57 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rodnaph-singer/health.svg)

```
[![Health](https://phpackages.com/badges/rodnaph-singer/health.svg)](https://phpackages.com/packages/rodnaph-singer)
```

###  Alternatives

[wpupdatephp/wp-update-php

Library to be bundled with WordPress plugins to enforce users to upgrade to PHP 5.4 hosting

7827.4k1](/packages/wpupdatephp-wp-update-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
