webservco / constant-value-class
A Constant Value Class implementation
v1.5.1
2024-06-22 20:13 UTC
Requires
- php: ^7.4||^8.1
Requires (Dev)
- pds/skeleton: ^1
- phan/phan: ^5
- php-parallel-lint/php-console-highlighter: ^1
- php-parallel-lint/php-parallel-lint: ^1
- phpcompatibility/php-compatibility: @dev
- phpmd/phpmd: ^2
- phpstan/phpstan: ^1
- phpstan/phpstan-strict-rules: ^1
- phpunit/phpunit: ^9
- slevomat/coding-standard: ^8
- squizlabs/php_codesniffer: ^3
- vimeo/psalm: ^4
Suggests
- phpdocumentor/phpdocumentor: phpDocumentor v3 (global installation)
README
A Constant Value Class implementation (enum simulation for PHP 7).
Based on the article "Expressive, type-checked constants for PHP".
Note: Only int
and string
values are supported.
Example
Constant Value Class
Please see tests/unit/WebServCo/ConstantValueClass/Example.php
Usage
class Shipment { public function send(Type $type): bool { // ... } } $shipment = new Shipment(); $shipment->send(Type::import()); // or $type = 2; // request parameter, user input, etc $shipment->send(Type::fromValue($type)); // get value (exact type) (eg. for form select) echo Type::import()->value(); // outputs "1" // get string representation of value echo Type::import(); // outputs "1" // comparison $import = Type::fromValue(2); if (Type::import() === $import) { // === works as long as the object is created in the current script run, // not for example created elsewhere and stored serialized in session. }