PHPackages                             xterr/classnames-php - 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. xterr/classnames-php

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

xterr/classnames-php
====================

A simple PHP utility for conditionally joining classNames together

v1.0.1(8y ago)032MITPHPPHP &gt;=5.3.0

Since Aug 24Pushed 8y ago1 watchersCompare

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

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

classnames-php
==============

[](#classnames-php)

A simple PHP utility for conditionally joining classNames together

[![Build Status](https://camo.githubusercontent.com/4da2f947dfb5a470792b0dbd69d1f9a080e897b651b27cf7dda71e61572e92b0/68747470733a2f2f7472617669732d63692e6f72672f78746572722f636c6173736e616d65732d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/xterr/classnames-php)

PHP port of the JavaScript classNames utility.

Inspired by [CJStroud/classnames-php](https://github.com/CJStroud/classnames-php)

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

[](#installation)

```
composer require xterr/classnames-php

```

The `classNames` can be accessed by using the function defined in `xterr\ClassNames\classNames`

```
use function xterr\ClassNames\classNames;

classNames('foo', ['bar' => TRUE]); // 'foo bar'
```

or by instantiating the class `\xterr\ClassNames\ClassNames` and using it as a function

```
use \xterr\ClassNames\ClassNames;

$oClassNames = new \xterr\ClassNames\ClassNames;
$oClassNames('foo', ['bar' => TRUE]); // 'foo bar'
```

Usage
-----

[](#usage)

The `classNames` function takes any number of arguments which can be a string or array. When using an array, if the value associated with a given key is falsy, that key won't be included in the output. If no value is given the true is assumed.

```
use function xterr\ClassNames\classNames;

classNames('foo'); // 'foo'
classNames(['foo' => TRUE]); // 'foo'
classNames('foo', ['bar' => FALSE, 'baz' => TRUE]); // 'foo baz'
classNames(['foo', 'bar' => TRUE]) // 'foo bar'

// Falsy values get ignored
classNames('foo', NULL, 0, FALSE, 1); // 'foo 1'
```

Arrays will be recursively flattened as per the rules above:

```
use function xterr\ClassNames\classNames;

$arr = ['b', ['c' => TRUE, 'd' => FALSE]];
classNames('a', arr); // => 'a b c'
```

Objects will be processed if the \_\_toString() method exists

```
use function xterr\ClassNames\classNames;

class ExampleObject
{
    function __toString()
    {
        return 'bar';
    }
}

classNames(new ExampleObject()); // => 'bar'
```

Functions and callables will be processed and should return the same types (string, array, etc) as the arguments accepted by the `classNames` function. The functions and callables will receive the entire result set as argument.

```
use function xterr\ClassNames\classNames;

class ExampleObject
{
    public static function getClasses($aResultSet)
    {
        return ['bar'];
    }

    public function getClassesDynamic($aResultSet)
    {
        return ['baz'];
    }
}

$oObj = new ExampleObject();

classNames(function($aResultSet) {
    return 'foo'
}, ['ExampleObject', 'getClasses'], [$oObj, 'getClassesDynamic']); // 'foo bar baz'
```

\##Dedupe

Dedupes classes and ensures that falsy classes specified in later arguments are excluded from the result set.

```
use function xterr\ClassNames\classNames;

classNames('foo', 'foo', 'bar'); // => 'foo bar'
classNames('foo', ['foo' => FALSE, 'bar' => TRUE ]); // => 'bar'
```

License
-------

[](#license)

[MIT](LICENSE). Copyright (c) 2017 Razvan Ceana.

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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 ~4 days

Total

2

Last Release

3229d ago

### Community

Maintainers

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

---

Top Contributors

[![xterr](https://avatars.githubusercontent.com/u/619509?v=4)](https://github.com/xterr "xterr (8 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/xterr-classnames-php/health.svg)

```
[![Health](https://phpackages.com/badges/xterr-classnames-php/health.svg)](https://phpackages.com/packages/xterr-classnames-php)
```

###  Alternatives

[jakubkulhan/chrome-devtools-protocol

Chrome Devtools Protocol client for PHP

189972.3k3](/packages/jakubkulhan-chrome-devtools-protocol)

PHPackages © 2026

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