-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgender.php
More file actions
65 lines (52 loc) · 1.28 KB
/
Copy pathgender.php
File metadata and controls
65 lines (52 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Zlikavac32\ZEnum\Examples;
use Zlikavac32\ZEnum\ZEnum;
require_once __DIR__ . '/../vendor/autoload.php';
/**
* More advanced example where you define common constructor and behaviour for every enum. Then enum objects are
* created with different arguments
*/
/**
* @method static Gender MALE
* @method static Gender FEMALE
*/
abstract class Gender extends ZEnum
{
/**
* @var string
*/
private $symbol;
public function __construct(string $symbol)
{
parent::__construct();
$this->symbol = $symbol;
}
protected static function enumerate(): array
{
//Create enums with different constructor arguments
return [
'MALE' => new class('M') extends Gender
{
},
'FEMALE' => new class('F') extends Gender
{
},
];
}
public function symbol(): string
{
return $this->symbol;
}
}
function dumpGender(Gender $gender)
{
var_dump($gender->symbol());
}
var_dump(
Gender::FEMALE()
->symbol()
); // string(1) "F"
var_dump(Gender::MALE() === Gender::FEMALE()); // bool(false)
var_dump(Gender::MALE() === Gender::MALE()); // bool(true)
dumpGender(Gender::MALE()); // string(1) "M"