@@ -479,6 +479,109 @@ class ClassName
479479
480480### 4.4 Property Hooks
481481
482+ Object properties may also include hooks, which have a number of syntactic options.
483+
484+ If there is only one hook implementation, and that hook implementation itself uses
485+ the short-hook syntax, then the hook declaration MAY be listed entirely inline. The hook
486+ name MUST be separated from the opening brace and the arrow operator by a single
487+ space, and the semicolon ending of the hook MUST be separated from the cosing brace
488+ by a single space. For example:
489+
490+ ``` php
491+ class Example
492+ {
493+ public string $myName { get => __CLASS__; }
494+
495+ public string $newName { set => ucfirst($value); }
496+ }
497+ ```
498+
499+ If those two criteria are not met, then the hook block MUST be spread across multiple lines:
500+
501+ ``` php
502+ class Example
503+ {
504+ public string $myName {
505+ get => __CLASS__;
506+ }
507+
508+ public string $newName {
509+ set => ucfirst($value);
510+ }
511+ }
512+ ```
513+
514+ When using the multi-line form, the opening brace MUST be on the same line as the property and
515+ the closing brace MUST be on its own line, with the body indented one level.
516+
517+ The ` => ` operator MUST have a single space on either side.
518+
519+ If multiple hooks are declared, they MUST be separated by at least a single line break. They
520+ MAY be separated by an additional blank line to aid readability.
521+
522+ If the property includes a default value, there MUST be a single space between the default value
523+ and the opening brace.
524+
525+ ``` php
526+ class Example
527+ {
528+ public string $newName = 'Me' {
529+ set => ucfirst($value);
530+ }
531+ }
532+ ```
533+
534+ If a hook is using its multi-line form, the opening brace MUST be on the same line as the hook
535+ name, and the closing brace MUST be on its own line. The body MUST be indented one level.
536+
537+ ``` php
538+ class Example
539+ {
540+ public string $newName = 'Me' {
541+ set {
542+ if (strlen($value) < 3) {
543+ throw new \Exception('Too short');
544+ }
545+ $this->newName = ucfirst($value);
546+ }
547+ }
548+ }
549+ ```
550+
551+ For a ` set ` hook, if the argument name and type do not need to be redefined, then they MAY be omitted.
552+
553+ Property hooks MAY also be defined in constructor-promoted properties. However, they
554+ MUST be only a single hook, with a short-syntax body, defined on a single line as above.
555+ If those criteria are not met, then the promoted property MUST NOT have any hooks defined
556+ inline, and SHOULD instead be defined as normal property and not promoted.
557+
558+ ``` php
559+ class Example
560+ {
561+ public function __construct(
562+ public string $name { set => ucfirst($value; }
563+ ) {}
564+ }
565+ ```
566+
567+ The following is *** not allowed*** due to the hook being too complex:
568+
569+ ``` php
570+ class Example
571+ {
572+ public function __construct(
573+ public string $name {
574+ set {
575+ if (strlen($value) < 3) {
576+ throw new \Exception('Too short');
577+ }
578+ $this->newName = ucfirst($value);
579+ }
580+ }
581+ ) {}
582+ }
583+ ```
584+
482585### 4.5 Methods and Functions
483586
484587Visibility MUST be declared on all methods.
0 commit comments