36
36
<programlisting role =" php" >
37
37
<![CDATA[
38
38
<?php
39
+
39
40
abstract class AbstractClass
40
41
{
41
- // Force Extending class to define this method
42
+ // Force extending class to define this method
42
43
abstract protected function getValue();
43
44
abstract protected function prefixValue($prefix);
44
45
45
46
// Common method
46
- public function printOut() {
47
+ public function printOut()
48
+ {
47
49
print $this->getValue() . "\n";
48
50
}
49
51
}
50
52
51
53
class ConcreteClass1 extends AbstractClass
52
54
{
53
- protected function getValue() {
55
+ protected function getValue()
56
+ {
54
57
return "ConcreteClass1";
55
58
}
56
59
57
- public function prefixValue($prefix) {
60
+ public function prefixValue($prefix)
61
+ {
58
62
return "{$prefix}ConcreteClass1";
59
63
}
60
64
}
61
65
62
66
class ConcreteClass2 extends AbstractClass
63
67
{
64
- public function getValue() {
68
+ public function getValue()
69
+ {
65
70
return "ConcreteClass2";
66
71
}
67
72
68
- public function prefixValue($prefix) {
73
+ public function prefixValue($prefix)
74
+ {
69
75
return "{$prefix}ConcreteClass2";
70
76
}
71
77
}
72
78
73
- $class1 = new ConcreteClass1;
79
+ $class1 = new ConcreteClass1() ;
74
80
$class1->printOut();
75
- echo $class1->prefixValue('FOO_') . "\n";
81
+ echo $class1->prefixValue('FOO_'), "\n";
76
82
77
- $class2 = new ConcreteClass2;
83
+ $class2 = new ConcreteClass2() ;
78
84
$class2->printOut();
79
- echo $class2->prefixValue('FOO_') ."\n";
85
+ echo $class2->prefixValue('FOO_'), "\n";
86
+
80
87
?>
81
88
]]>
82
89
</programlisting >
@@ -96,32 +103,34 @@ FOO_ConcreteClass2
96
103
<programlisting role =" php" >
97
104
<![CDATA[
98
105
<?php
106
+
99
107
abstract class AbstractClass
100
108
{
101
- // Our abstract method only needs to define the required arguments
109
+ // An abstract method only needs to define the required arguments
102
110
abstract protected function prefixName($name);
103
-
104
111
}
105
112
106
113
class ConcreteClass extends AbstractClass
107
114
{
108
-
109
- // Our child class may define optional arguments not in the parent's signature
110
- public function prefixName($name, $separator = ".") {
115
+ // A child class may define optional parameters which are not present in the parent's signature
116
+ public function prefixName($name, $separator = ".")
117
+ {
111
118
if ($name == "Pacman") {
112
119
$prefix = "Mr";
113
120
} elseif ($name == "Pacwoman") {
114
121
$prefix = "Mrs";
115
122
} else {
116
123
$prefix = "";
117
124
}
125
+
118
126
return "{$prefix}{$separator} {$name}";
119
127
}
120
128
}
121
129
122
- $class = new ConcreteClass;
130
+ $class = new ConcreteClass() ;
123
131
echo $class->prefixName("Pacman"), "\n";
124
132
echo $class->prefixName("Pacwoman"), "\n";
133
+
125
134
?>
126
135
]]>
127
136
</programlisting >
@@ -138,35 +147,44 @@ Mrs. Pacwoman
138
147
<programlisting role =" php" >
139
148
<![CDATA[
140
149
<?php
150
+
141
151
abstract class A
142
152
{
143
- // Extending classes must have a publicly-gettable property.
144
- abstract public string $readable { get; }
153
+ // Extending classes must have a publicly-gettable property
154
+ abstract public string $readable {
155
+ get;
156
+ }
145
157
146
- // Extending classes must have a protected- or public-writeable property.
147
- abstract protected string $writeable { set; }
158
+ // Extending classes must have a protected- or public-writeable property
159
+ abstract protected string $writeable {
160
+ set;
161
+ }
148
162
149
- // Extending classes must have a protected or public symmetric property.
150
- abstract protected string $both { get; set; }
163
+ // Extending classes must have a protected or public symmetric property
164
+ abstract protected string $both {
165
+ get;
166
+ set;
167
+ }
151
168
}
152
169
153
170
class C extends A
154
171
{
155
- // This satisfies the requirement and also makes it settable, which is valid.
172
+ // This satisfies the requirement and also makes it settable, which is valid
156
173
public string $readable;
157
174
158
- // This would NOT satisfy the requirement, as it is not publicly readable.
175
+ // This would NOT satisfy the requirement, as it is not publicly readable
159
176
protected string $readable;
160
177
161
178
// This satisfies the requirement exactly, so is sufficient.
162
- // It may only be written to, and only from protected scope.
179
+ // It may only be written to, and only from protected scope
163
180
protected string $writeable {
164
181
set => $value;
165
182
}
166
183
167
- // This expands the visibility from protected to public, which is fine.
184
+ // This expands the visibility from protected to public, which is fine
168
185
public string $both;
169
186
}
187
+
170
188
?>
171
189
]]>
172
190
</programlisting >
@@ -180,15 +198,20 @@ class C extends A
180
198
<programlisting role =" php" >
181
199
<![CDATA[
182
200
<?php
201
+
183
202
abstract class A
184
203
{
185
204
// This provides a default (but overridable) set implementation,
186
- // and requires child classes to provide a get implementation.
205
+ // and requires child classes to provide a get implementation
187
206
abstract public string $foo {
188
207
get;
189
- set { $this->foo = $value };
208
+
209
+ set {
210
+ $this->foo = $value;
211
+ }
190
212
}
191
213
}
214
+
192
215
?>
193
216
]]>
194
217
</programlisting >
0 commit comments