/
024.php
105 lines (97 loc) · 2.52 KB
/
024.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
error_reporting(-1);
/* selfキーワード */
class SelfClass
{
public const STR = 'string';
private static int $num = 999;
protected static function tested()
{
echo __METHOD__ , "\n";
}
public static function test()
{
// クラス名を使ってcallする
SelfClass::tested();
// selfキーワードを使ってcallする
self::tested();
// 定数やプロパティも同様にアクセスできる
var_dump(self::STR);
var_dump(self::$num);
}
}
//
SelfClass::test();
/* 遅延静的束縛 */
/* staticではないクラスでの継承(意図通り、を想定) */
class DynamicHoge
{
protected function tested()
{
echo __METHOD__ , "\n";
}
public function test()
{
$this->tested();
}
}
class DynamicFoo extends DynamicHoge
{
protected function tested()
{
echo __METHOD__ , "\n";
}
}
// test()メソッドで呼ばれるtestedは、DynamicHogeクラス側の実装
(new DynamicHoge())->test();
// test()メソッドで呼ばれるtestedは、DynamicFooクラス側の実装
(new DynamicFoo())->test();
/* staticなクラスでの継承(意図通りではない、を想定) */
class SelfHoge
{
protected static function tested()
{
echo __METHOD__ , "\n";
}
public static function test()
{
// XXX selfなので「常に自クラス(SelfHoge)」を指し示す
self::tested();
}
}
class SelfFoo extends SelfHoge
{
protected static function tested()
{
echo __METHOD__ , "\n";
}
}
// test()メソッドで呼ばれるtestedは、SelfHogeクラス側の実装(こちらは意図通り)
SelfHoge::test();
// test()メソッドで呼ばれるtestedは、(SelfFooではなく)SelfHogeクラス側の実装(こちらが意図通りではない可能性)
SelfFoo::test();
/* staticなクラスでの継承(意図通り、を想定) */
class StaticHoge
{
protected static function tested()
{
echo __METHOD__ , "\n";
}
public static function test()
{
// XXX staticなので「どのクラスとして呼ばれたか」を判断する
static::tested();
}
}
class StaticFoo extends StaticHoge
{
protected static function tested()
{
echo __METHOD__ , "\n";
}
}
// test()メソッドで呼ばれるtestedは、SelfHogeクラス側の実装
StaticHoge::test();
// test()メソッドで呼ばれるtestedは、SelfFooクラス側の実装
StaticFoo::test();