1. Go to this page and download the library: Download cloudstek/php-enum library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Cloudstek\Enum\Enum;
class FooEnum extends Enum
{
private const FOO = 'foo';
}
class BarEnum extends FooEnum
{
private const BAR = 'bar';
}
class Foo
{
public function doSomething(FooEnum $foo)
{
// Do something...
}
}
$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // Allowed and OK, we were expecting FooEnum
$foo->doSomething(BarEnum::BAR()); // Allowed but not OK, we got BarEnum!
use Cloudstek\Enum\Enum;
abstract class BaseEnum extends Enum
{
private const HELLO = 'world';
}
final class FooEnum extends BaseEnum
{
private const FOO = 'foo';
}
final class BarEnum extends BaseEnum
{
private const BAR = 'bar';
}
class Foo
{
public function doSomething(FooEnum $foo)
{
// Do something...
}
}
$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // Allowed and OK, we were expecting FooEnum
$foo->doSomething(BarEnum::BAR()); // Fatal error
class Foo
{
public function doSomething(BaseEnum $foo)
{
// Do something...
}
}
$foo = new Foo();
$foo->doSomething(FooEnum::FOO()); // OK
$foo->doSomething(BarEnum::BAR()); // OK