-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_class_property.php
52 lines (35 loc) · 953 Bytes
/
template_class_property.php
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
<?php
declare(strict_types=1);
/**
* @template T
*/
class ValueHolder
{
/** @var T */
private $value;
/** @param T $value */
public function __construct($value)
{
$this->value = $value;
}
/** @return T */
public function value()
{
return $this->value;
}
}
function takesInt(int $int): void {}
function takesString(string $string): void {}
class Entity {
/** @var ValueHolder<int> */
public ValueHolder $ageValueHolder;
public function __construct()
{
$this->ageValueHolder = new ValueHolder(24);
}
}
$entity = new Entity();
takesInt($entity->ageValueHolder->value()); // OK
takesString($entity->ageValueHolder->value()); // ERROR. Passing int, expects string.
$entity->ageValueHolder = new ValueHolder(33); // OK
$entity->ageValueHolder = new ValueHolder("hello"); // ERROR. Assigning ValueHolder<string> to property that should be ValueHolder<int>