BrisPHP Q1 2022: RFC Discussion

Sealed classes (Apr 2021)

  • Inheritance allows code reuse
  • Having a parent class in a library allows users to extend that class
  • PHP has \Throwable and \DateTimeInterface that are already "sealed"
sealed class Option permits Some, None { ... }
 
class Some extends Option { ... } // ok
class None extends Option { ... } // ok
 
class Maybe extends Option { ... } // Fatal error: cannot extend sealed class

Pros

  • Library maintainers prevent users extending things they shouldn't be
  • Is a language feature and thus should not be an attribute

Cons

  • "sealed" and "permits" become invalid class names
  • I easier/nicer to implement as an attribute
  • Prevents users from doing things the author didn't consider (proxy/mock)
  • Already have @internal

Sealed classes (Apr 2021)

Readonly classes (Aug 2021)

readonly class Foo
{
    public int $bar;
 
    public function __construct() {
        $this->bar = 1;
    }
}
 
$foo = new Foo();
$foo->bar = 2;
// Fatal Error: Uncaught Error: Cannot modify readonly property Foo::$bar
 
$foo->baz = 1;
// Fatal Error: Uncaught Error: Cannot create dynamic property Foo::$baz
  • Declare all properties as readonly
  • Prevent dynamic properties
  • Already had a vote, but cancelled

Pros

  • Easier to declare all properties as readonly

Cons

  • Entirely syntactic sugar
  • Can't mark a class as "readonly except for $x"

Readonly classes (Aug 2021)

// As of 8.0:
 
$y = 1;
 
$fn1 = fn($x) => $x + $y; // auto-capture + single expression
 
$fn2 = function ($x) use ($y): int { // manual-capture + statement list
   // ...
 
   return $x + $y;
};

// With multi line arrow fns
$fn3 = fn ($x): int { // auto-capture + statement list
    // ...
 
    return $x + $y;
};

Multi line arrow fn (March 2021)

Pros

  • Consistent with other language features
  • Very useful

Cons

  • Automatic capture is controversial
    • Variables are not imported from other scopes
    • Being explicit is "better"
  • Syntax is controversial
  • Cannot use variable by reference
  • Will be used even when auto-capture is not required

Multi line arrow fn (March 2021)

BrisPHP Q1 2022: RFC Discussion

By Nathan Dench

BrisPHP Q1 2022: RFC Discussion

  • 258