oihana/php-traits

The Oihana PHP Traits library

Maintainers

Package info

github.com/BcommeBois/oihana-php-traits

pkg:composer/oihana/php-traits

Statistics

Installs: 4

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-06-19 15:53 UTC

This package is auto-updated.

Last update: 2026-06-19 16:26:23 UTC


README

Oihana PHP Traits

Reusable, composable object traits for PHP 8.4+.

Latest Version
Total Downloads
License

๐Ÿ“š Documentation

User guides (FR + EN), with narrative explanations and examples:

๐Ÿ‡ฌ๐Ÿ‡ง English documentation ๐Ÿ‡ซ๐Ÿ‡ท Documentation franรงaise
Getting started, container, identifiers, key-value, lazy/lockable, URI, strings, tips. Dรฉmarrage, conteneur, identifiants, clรฉ-valeur, lazy/lockable, URI, chaรฎnes, astuces.

Auto-generated API reference (phpDocumentor):
๐Ÿ‘‰ https://bcommebois.github.io/oihana-php-traits

๐Ÿง  New to traits?

A trait is a reusable block of methods and properties that you "mix into" a class with the use keyword. It is a way to share behaviour across classes that don't share a common parent โ€” without copy-pasting and without inheritance.

trait GreetTrait
{
    public function hello(): string
    {
        return "Hi, I'm {$this->name}";
    }
}

class User
{
    use GreetTrait; // โ† the class instantly gains the hello() method

    public function __construct( public string $name ) {}
}

echo ( new User('Ada') )->hello(); // Hi, I'm Ada

This package is a curated set of small, focused, tested traits: drop one into your class with use and get container access, identifiers, lazy/lockable state, key-value access, and more โ€” for free.

๐Ÿš€ Features

  • ๐Ÿงฉ DI-container awareness โ€” ContainerTrait.
  • ๐Ÿ’ค Lazy and ๐Ÿ”’ lockable state โ€” LazyTrait, LockableTrait.
  • ๐Ÿ†” Identifiers โ€” IDTrait, QueryIDTrait.
  • ๐Ÿ—‚๏ธ Config and key-value access โ€” ConfigTrait, KeyValueTrait, JsonOptionsTrait.
  • ๐Ÿ”— URI building, stringable and expression helpers โ€” UriTrait, ToStringTrait, strings\ExpressionTrait.
  • ๐Ÿ”ข Default sorting and unsupported-operation guards โ€” SortDefaultTrait, UnsupportedTrait.
  • ๐Ÿงช Full unit-test coverage ensuring reliability and maintainability.

๐Ÿ’ก Designed to be lightweight, testable, and compatible with any PHP 8.4+ project.

๐Ÿ“ฆ Installation

Requires PHP 8.4+

Install via Composer:

composer require oihana/php-traits

โœ… Tests & coverage

Run the full unit-test suite (PHPUnit, strict mode):

composer test

Run a single test case:

./vendor/bin/phpunit --filter ContainerTraitTest

Measure coverage (requires Xdebug or PCOV):

composer coverage        # text + Clover + HTML under build/coverage/
composer coverage:md     # readable Markdown summary (build/coverage/COVERAGE.md)

The suite runs in strict mode and targets 100% line coverage.

๐Ÿงพ License

This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).

๐Ÿ‘ค About the author

๐Ÿ› ๏ธ Generate the Documentation

We use phpDocumentor to generate the documentation into the ./docs folder.

Usage

Run the command :

composer doc

๐Ÿ”— Related packages