Skip to content

Commit ad331ce

Browse files
authored
Change asymmetric visibility example to show how it simplifies code (#1135)
The previous example wasn't very helpful, since it only showed invalid code that will error (and could just as easily be replaced with a readonly property).
1 parent b672403 commit ad331ce

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

releases/8.4/release.inc

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,20 @@ PHP
158158
<<<'PHP'
159159
class PhpVersion
160160
{
161-
public string $version = '8.3';
162-
}
161+
private string $version = '8.3';
163162
164-
$phpVersion = new PhpVersion();
165-
var_dump($phpVersion->version); // string(3) "8.3"
166-
$phpVersion->version = 'PHP 8.4'; // No error
163+
public function getVersion(): string
164+
{
165+
return $this->version;
166+
}
167+
168+
public function increment(): void
169+
{
170+
[$major, $minor] = explode('.', $this->version);
171+
$minor++;
172+
$this->version = "$major.$minor";
173+
}
174+
}
167175
PHP
168176

169177
); ?>
@@ -178,11 +186,14 @@ PHP
178186
class PhpVersion
179187
{
180188
public private(set) string $version = '8.4';
181-
}
182189
183-
$phpVersion = new PhpVersion();
184-
var_dump($phpVersion->version); // string(3) "8.4"
185-
$phpVersion->version = 'PHP 8.3'; // Visibility error
190+
public function increment(): void
191+
{
192+
[$major, $minor] = explode('.', $this->version);
193+
$minor++;
194+
$this->version = "$major.$minor";
195+
}
196+
}
186197
PHP
187198
); ?>
188199
</div>

0 commit comments

Comments
 (0)