oihana / php-traits
The Oihana PHP Traits library
Requires
- php: >=8.4
- oihana/php-core: dev-main
- oihana/php-enums: dev-main
- oihana/php-exceptions: dev-main
- oihana/php-reflect: dev-main
- php-di/php-di: ^7.0
- psr/container: ^2.0
Requires (Dev)
- nunomaduro/collision: ^8.8
- phpdocumentor/shim: ^3.8
- phpunit/phpunit: ^12
README
Reusable, composable object traits for PHP 8.4+.
๐ 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
- Author : Marc ALCARAZ (aka eKameleon)
- Mail : marc@ooop.fr
- Website : http://www.ooop.fr
๐ ๏ธ Generate the Documentation
We use phpDocumentor to generate the documentation into the ./docs folder.
Usage
Run the command :
composer doc
๐ Related packages
- oihana/php-core โ core helpers and utilities used by this library.
- oihana/php-enums โ a collection of strongly-typed constant enumerations for PHP.
- oihana/php-reflect โ reflection and hydration utilities.
