diff --git a/language/oop5/property-hooks.xml b/language/oop5/property-hooks.xml index 5550d52e94701..aa97d67d217f1 100644 --- a/language/oop5/property-hooks.xml +++ b/language/oop5/property-hooks.xml @@ -72,7 +72,7 @@ print $example->foo; The set hook additionally allows specifying the type and name of an incoming value, - using the same syntax as a method. The type must be either the same as the type of the property, + using the same syntax as a method. The type must be either the same as the type of the property, or contravariant (wider) to it. For instance, a property of type string could have a set hook that accepts string|Stringable, @@ -81,7 +81,7 @@ print $example->foo; At least one of the hooks references $this->foo, the property itself. That means the property wll be "backed." When calling $example->foo = 'changed', - the provided string will be first cast to lowercase, then saved to the backing value. + the provided string will be first cast to lowercase, then saved to the backing value. When reading from the property, the previously saved value may conditionally be appended with additional text. @@ -103,7 +103,7 @@ class Example public string $foo = 'default value' { get => $this->foo . ($this->modified ? ' (modified)' : ''); - + set(string $value) { $this->foo = strtolower($value); $this->modified = true; @@ -118,7 +118,7 @@ class Example This example is equivalent to the previous. - If the set hook's parameter type is the same as the property type (which is typical), + If the set hook's parameter type is the same as the property type (which is typical), it may be omitted. In that case, the value to set is automatically given the name $value. @@ -132,7 +132,7 @@ class Example public string $foo = 'default value' { get => $this->foo . ($this->modified ? ' (modified)' : ''); - + set { $this->foo = strtolower($value); $this->modified = true; @@ -258,11 +258,11 @@ class Person { public string $phone { set => $this->sanitizePhone($value); } - + private function sanitizePhone(string $value): string { $value = ltrim($value, '+'); $value = ltrim($value, '1'); - + if (!preg_match('/\d\d\d\-\d\d\d\-\d\d\d\d/', $value)) { throw new \InvalidArgumentException(); }