Author: Theodore Brown (theodorejb)
Committer: GitHub (web-flow)
Pusher: saundefined
Date: 2024-11-21T08:05:29+03:00
Commit: Change asymmetric visibility example to show how it simplifies code (… · php/web-php@ad331ce · GitHub
Raw diff: https://github.com/php/web-php/commit/ad331ce96e1fe8600c1e5284ac410f402173a0aa.diff
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).
Changed paths:
M releases/8.4/release.inc
Diff:
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
); ?>
</div>