From ff4b85e81e20d9d7070eef8480645e545d51551c Mon Sep 17 00:00:00 2001 From: Theodore Brown Date: Wed, 20 Nov 2024 15:59:48 -0600 Subject: [PATCH] Change asymmetric visibility example to show how it simplifies code 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). --- releases/8.4/release.inc | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/releases/8.4/release.inc b/releases/8.4/release.inc index 04daaebe24..dd1f76310a 100644 --- a/releases/8.4/release.inc +++ b/releases/8.4/release.inc @@ -158,12 +158,20 @@ PHP <<<'PHP' class PhpVersion { - public string $version = '8.3'; -} + private string $version = '8.3'; -$phpVersion = new PhpVersion(); -var_dump($phpVersion->version); // string(3) "8.3" -$phpVersion->version = 'PHP 8.4'; // No error + public function getVersion(): string + { + return $this->version; + } + + public function increment(): void + { + [$major, $minor] = explode('.', $this->version); + $minor++; + $this->version = "$major.$minor"; + } +} PHP ); ?> @@ -178,11 +186,14 @@ PHP class PhpVersion { public private(set) string $version = '8.4'; -} -$phpVersion = new PhpVersion(); -var_dump($phpVersion->version); // string(3) "8.4" -$phpVersion->version = 'PHP 8.3'; // Visibility error + public function increment(): void + { + [$major, $minor] = explode('.', $this->version); + $minor++; + $this->version = "$major.$minor"; + } +} PHP ); ?>