Skip to content

Instantly share code, notes, and snippets.

@DaveLiddament
Created May 27, 2022 10:49
Show Gist options
  • Save DaveLiddament/18f924f466eba6c4ebd5d03cd6c80782 to your computer and use it in GitHub Desktop.
Save DaveLiddament/18f924f466eba6c4ebd5d03cd6c80782 to your computer and use it in GitHub Desktop.
Example of using the #[Friend] attribute from the PHP Language extensions library
// Example of the #[Friend] attribute available from https://github.com/DaveLiddament/php-language-extensions
class Person {
#[Friend(PersonBuilder::class)]
public function __construct() {}
}
class PersonBuilder {
public function build(): Person {
// OK: PersonBuilder is a friend of Person::__construct
return new Person():
}
}
class AnotherClass {
public function doSomething(): void {
// Error: Person::__construct can only be called by its friend: PersonBuilder
$person = new Person();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment