webservco/constant-value-class

v1.5.1 2024-06-22 20:13 UTC

This package is auto-updated.

Last update: 2024-09-22 20:44:55 UTC


README

常量值类实现(PHP 7的枚举模拟)。

基于文章"PHP的具有类型检查的常量表达方式"

注意:仅支持intstring值。

示例

常量值类

请参阅tests/unit/WebServCo/ConstantValueClass/Example.php

用法

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.
}