You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spec.md
+55-36Lines changed: 55 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -777,22 +777,59 @@ If so, the `...` MUST NOT include any whitespace before or after. That is, the c
777
777
778
778
Object properties may also include hooks, which have a number of syntactic options.
779
779
780
-
If there is only one hook implementation, and that hook implementation itself uses
781
-
the short-hook syntax, then the hook declaration MAY be listed entirely inline. The hook
782
-
name MUST be separated from the opening brace and the arrow operator by a single
783
-
space, and the semicolon ending of the hook MUST be separated from the closing brace
784
-
by a single space. For example:
780
+
When using the long form of hooks:
781
+
782
+
* The opening brace MUST be on the same line as the property.
783
+
* The opening brace MUST be separated from the property name or its default value by a single space.
784
+
* The closing brace MUST be on its own line, and have no comment following it.
785
+
* The entire body of the hook definition MUST be indented one level.
786
+
* The body of each hook MUST be indented one level.
787
+
* If multiple hooks are declared, they MUST be separated by at least a single line break. They
788
+
MAY be separated by an additional blank line to aid readability.
789
+
790
+
For example:
785
791
786
792
```php
787
793
class Example
788
794
{
789
-
public string $myName { get => __CLASS__; }
795
+
public string $newName = 'Me' {
796
+
set {
797
+
if (strlen($value) < 3) {
798
+
throw new \Exception('Too short');
799
+
}
800
+
$this->newName = ucfirst($value);
801
+
}
802
+
}
790
803
791
-
public string $newName { set => ucfirst($value); }
804
+
public string $department {
805
+
get {
806
+
return $this->values[__PROPERTY__];
807
+
}
808
+
set {
809
+
$this->values[__PROPERTY__] = $value;
810
+
}
811
+
}
812
+
// or
813
+
public string $department {
814
+
get {
815
+
return $this->values[__PROPERTY__];
816
+
}
817
+
818
+
set {
819
+
$this->values[__PROPERTY__] = $value;
820
+
}
821
+
}
792
822
}
793
823
```
794
824
795
-
If those two criteria are not met, then the hook block MUST be spread across multiple lines:
825
+
Property hooks also support multiple short-hook variations.
826
+
827
+
For a `set` hook, if the argument name and type do not need to be redefined, then they MAY be omitted.
828
+
829
+
If a hook consists of a single expression, then PHP allows it to be shortened using `=>`. In that case:
830
+
831
+
* There MUST be a single space on either side of the `=>` symbol.
832
+
* The body MUST be on the same line as the hook declaration, unless it gets prohibitively long. If it gets prohibitively long, the developer SHOULD consider not using the short-hook syntax.
796
833
797
834
```php
798
835
class Example
@@ -807,45 +844,27 @@ class Example
807
844
}
808
845
```
809
846
810
-
When using the multi-line form, the opening brace MUST be on the same line as the property and
811
-
the closing brace MUST be on its own line, with the body indented one level.
812
-
813
-
The `=>` operator MUST have a single space on either side.
847
+
Additionally, if the following criteria are met:
814
848
815
-
If multiple hooks are declared, they MUST be separated by at least a single line break. They
816
-
MAY be separated by an additional blank line to aid readability.
849
+
* There is only one hook implementation.
850
+
* That hook uses the short-hook syntax.
817
851
818
-
If the property includes a default value, there MUST be a single space between the default value
819
-
and the opening brace.
852
+
Then the hook MAY be listed entirely inline. In that case,
820
853
821
-
```php
822
-
class Example
823
-
{
824
-
public string $newName = 'Me' {
825
-
set => ucfirst($value);
826
-
}
827
-
}
828
-
```
854
+
* The hook name MUST be separated from the opening brace and the arrow operator by a single space
855
+
* The semicolon ending of the hook MUST be separated from the closing brace by a single space.
829
856
830
-
If a hook is using its multi-line form, the opening brace MUST be on the same line as the hook
831
-
name, and the closing brace MUST be on its own line. The body MUST be indented one level.
857
+
For example:
832
858
833
859
```php
834
860
class Example
835
861
{
836
-
public string $newName = 'Me' {
837
-
set {
838
-
if (strlen($value) < 3) {
839
-
throw new \Exception('Too short');
840
-
}
841
-
$this->newName = ucfirst($value);
842
-
}
843
-
}
862
+
public string $myName { get => __CLASS__; }
863
+
864
+
public string $newName { set => ucfirst($value); }
844
865
}
845
866
```
846
867
847
-
For a `set` hook, if the argument name and type do not need to be redefined, then they MAY be omitted.
848
-
849
868
Property hooks MAY also be defined in constructor-promoted properties. However, they
850
869
MUST be only a single hook, with a short-syntax body, defined on a single line as above.
851
870
If those criteria are not met, then the promoted property MUST NOT have any hooks defined
0 commit comments