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

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

pangora/classnames
==================

Elegant classlist builder with conditionals

1.0.1(5y ago)061MITPHPPHP ^7.4|^8.0

Since Oct 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/pangora/classnames)[ Packagist](https://packagist.org/packages/pangora/classnames)[ Docs](https://github.com/pangora/classnames)[ RSS](/packages/pangora-classnames/feed)WikiDiscussions main Synced 3d ago

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

[![Classnames banner](/art/banner.png)](/art/banner.png)

Classnames for PHP
==================

[](#classnames-for-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/48c41a8a903024eee56a93954bb9450b5ac1452a572ee6db54568331861b64b7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70616e676f72612f636c6173736e616d65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pangora/classnames)[![Build Status](https://camo.githubusercontent.com/9779cbdce8c3621676a01f5ff4e60a8d95c23a5f7e5f84608e63a25bb6738f00/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f656e756d2f72756e2d74657374733f6c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/spatie/enum/actions?query=workflow%3Arun-tests)[![Total Downloads](https://camo.githubusercontent.com/4ba67ca69b913f224e02ec38cfe35974364c785701eed5f723782d2cde52fd5b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70616e676f72612f636c6173736e616d65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pangora/classnames)

This package replicates the functionality of the popular JS package [JedWatson/classnames](https://github.com/JedWatson/classnames), allowing you to easily build HTML classlists using strings, stringable objects and arrays with conditions, like this:

```
Classnames::from(
    'btn',
    ['btn-primary'],
    ['btn-secondary' => false],
    ['btn-wide' => true],
    new StringableObject('btn-lg')
);
// => 'btn btn-primary btn-wide btn-lg'
```

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

[](#installation)

You can install the package via composer:

```
composer require pangora/classnames
```

Usage
-----

[](#usage)

Classnames accepts multiple arguments through the static `from` method and it'll respect the argument order when building the classlist. The `from` method won't remove duplicates, but you may use the static `dedupeFrom`, which works the same way, while also removing duplicates.

The allowed argument types are:

- `string`
- `int` (will be converted to `string`)
- Any "stringable" object implementing the magic `__toString()` method.
- Sequential arrays
- Associative arrays where the key represents the value and the value represent the condition.

Arguments of other types will be ignored, except for multidimensional array which will throw an exception.

```
Classnames::from('btn btn-primary');
// => 'btn btn-primary'

Classnames::from('btn', 'btn-primary');
// => 'btn btn-primary'

Classnames::from('   lots of ', ' space  ');
// => 'lots of space'

Classnames::from('btn', ['btn-primary']);
// => 'btn btn-primary'

Classnames::from([
    'btn' => false,
    'btn-secondary' => false,
    'btn-primary' => true,
]);
// => 'btn btn-primary'

Classnames::from(
    'card',
    new StringableObject('card-lg')
);
// => 'card card-lg'
```

You may also deduplicate the classlist using `dedupefrom`:

```
Classnames::dedupeFrom('a btn b btn c');
// => 'a btn b c'
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Jarand K. Løkeland](https://github.com/pangora)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

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

Total

2

Last Release

1912d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3435778d45c944584403af1dd46d18cbd31bfed0bf6f29241c0385d737011e95?d=identicon)[lokeland](/maintainers/lokeland)

---

Top Contributors

[![lokeland](https://avatars.githubusercontent.com/u/6294253?v=4)](https://github.com/lokeland "lokeland (7 commits)")

---

Tags

phputilityclassnamespangora

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[bdelespierre/underscore

Underscore.js port in PHP

6943.7k1](/packages/bdelespierre-underscore)

PHPackages © 2026

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