Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Generated on May 16, 2024 via composer

myclabs/php-enum 1.8.4

PHP Enum implementation
Package summary
Share
0
issues
1
license
1
MIT
Package created
20 Mar 2013
Version published
4 Aug 2022
Maintainers
2
Total deps
1
Direct deps
0
License
MIT

Issues

0
This package has no issues

Licenses

MIT License

Permissive
OSI Approved
This is a human-readable summary of (and not a substitute for) the license. Disclaimer.
Can
commercial-use
modify
distribute
sublicense
private-use
Cannot
hold-liable
Must
include-copyright
include-license
1 Packages, Including:
myclabs/php-enum@1.8.4
Disclaimer

This deed highlights only some of the key features and terms of the actual license. It is not a license and has no legal value. You should carefully review all of the terms and conditions of the actual license before using the licensed material.

Sandworm is not a law firm and does not provide legal services. Distributing, displaying, or linking to this deed or the license that it summarizes does not create a lawyer-client or any other relationship.

Direct Dependencies

0
All Dependencies CSV
β“˜ This is a list of myclabs/php-enum 's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.
NameVersionSizeLicenseTypeVulnerabilities

Visualizations

Frequently Asked Questions

What does myclabs/php-enum do?

The MyCLabs PHP Enum package provides a way for PHP developers to use enumerations, a concept not integrated into the PHP core, avoiding the need to separately install the SplEnum extension. Enumerations allow you to define a type that can be one of several different values, providing more type-safety than using class constants. This package enables developers to use an enumeration as a parameter type or return type, enrich the enumeration with methods, extend the enumeration to add new values, and obtain a list of all possible values.

How do you use myclabs/php-enum?

To use the MyCLabs PHP Enum package, start by installing it into your PHP project using composer with the command composer require myclabs/php-enum. Then, declare an enumeration, which extends MyCLabs\Enum\Enum, using private constants to describe the possible values.

use MyCLabs\Enum\Enum;

/**
 * Action enum
 */
final class Action extends Enum
{
    private const VIEW = 'view';
    private const EDIT = 'edit';
}

To use the enumeration, you can use the statically implemented methods to quickly access an enum value, such as Action::VIEW(). It's also possible to use enums as a parameter type in functions.

$action = Action::VIEW();

function setAction(Action $action) {
    // ...
}

Where are the myclabs/php-enum docs?

The documentation for the MyCLabs PHP Enum package can be found right in the README file of the repository. Here, you'll find details about the package's constructor, built-in methods and static methods. For instance, the __construct() method ensures the value exists in the enum, while the __toString() method allows an enum value to be output as a string.

The static methods provide a variety of utilities such as from() to create an enum instance, toArray() to return all possible values, keys() to return all possible keys (names of constants), values() to return instances for all constants, among others. It also includes methods to verify value validity (isValid(), isValidKey(), assertValidValue()) and search for a key (search()).

Should your project be running PHP 8.1 or higher, you should consider using the newly introduced native enums instead of this library. A comparison and guide to migrating to native enums is provided within the documentation.