3636 <programlisting role =" php" >
3737<![CDATA[
3838<?php
39+
3940abstract class AbstractClass
4041{
41- // Force Extending class to define this method
42+ // Force extending class to define this method
4243 abstract protected function getValue();
4344 abstract protected function prefixValue($prefix);
4445
4546 // Common method
46- public function printOut() {
47+ public function printOut()
48+ {
4749 print $this->getValue() . "\n";
4850 }
4951}
5052
5153class ConcreteClass1 extends AbstractClass
5254{
53- protected function getValue() {
55+ protected function getValue()
56+ {
5457 return "ConcreteClass1";
5558 }
5659
57- public function prefixValue($prefix) {
60+ public function prefixValue($prefix)
61+ {
5862 return "{$prefix}ConcreteClass1";
5963 }
6064}
6165
6266class ConcreteClass2 extends AbstractClass
6367{
64- public function getValue() {
68+ public function getValue()
69+ {
6570 return "ConcreteClass2";
6671 }
6772
68- public function prefixValue($prefix) {
73+ public function prefixValue($prefix)
74+ {
6975 return "{$prefix}ConcreteClass2";
7076 }
7177}
7278
73- $class1 = new ConcreteClass1;
79+ $class1 = new ConcreteClass1() ;
7480$class1->printOut();
75- echo $class1->prefixValue('FOO_') . "\n";
81+ echo $class1->prefixValue('FOO_'), "\n";
7682
77- $class2 = new ConcreteClass2;
83+ $class2 = new ConcreteClass2() ;
7884$class2->printOut();
79- echo $class2->prefixValue('FOO_') ."\n";
85+ echo $class2->prefixValue('FOO_'), "\n";
86+
8087?>
8188]]>
8289 </programlisting >
@@ -96,32 +103,34 @@ FOO_ConcreteClass2
96103 <programlisting role =" php" >
97104<![CDATA[
98105<?php
106+
99107abstract class AbstractClass
100108{
101- // Our abstract method only needs to define the required arguments
109+ // An abstract method only needs to define the required arguments
102110 abstract protected function prefixName($name);
103-
104111}
105112
106113class ConcreteClass extends AbstractClass
107114{
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+ {
111118 if ($name == "Pacman") {
112119 $prefix = "Mr";
113120 } elseif ($name == "Pacwoman") {
114121 $prefix = "Mrs";
115122 } else {
116123 $prefix = "";
117124 }
125+
118126 return "{$prefix}{$separator} {$name}";
119127 }
120128}
121129
122- $class = new ConcreteClass;
130+ $class = new ConcreteClass() ;
123131echo $class->prefixName("Pacman"), "\n";
124132echo $class->prefixName("Pacwoman"), "\n";
133+
125134?>
126135]]>
127136 </programlisting >
@@ -138,35 +147,44 @@ Mrs. Pacwoman
138147 <programlisting role =" php" >
139148<![CDATA[
140149<?php
150+
141151abstract class A
142152{
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+ }
145157
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+ }
148162
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+ }
151168}
152169
153170class C extends A
154171{
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
156173 public string $readable;
157174
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
159176 protected string $readable;
160177
161178 // 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
163180 protected string $writeable {
164181 set => $value;
165182 }
166183
167- // This expands the visibility from protected to public, which is fine.
184+ // This expands the visibility from protected to public, which is fine
168185 public string $both;
169186}
187+
170188?>
171189]]>
172190 </programlisting >
@@ -180,15 +198,20 @@ class C extends A
180198 <programlisting role =" php" >
181199<![CDATA[
182200<?php
201+
183202abstract class A
184203{
185204 // 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
187206 abstract public string $foo {
188207 get;
189- set { $this->foo = $value };
208+
209+ set {
210+ $this->foo = $value;
211+ }
190212 }
191213}
214+
192215?>
193216 ]]>
194217 </programlisting >
0 commit comments