Created
May 27, 2022 10:49
-
-
Save DaveLiddament/18f924f466eba6c4ebd5d03cd6c80782 to your computer and use it in GitHub Desktop.
Example of using the #[Friend] attribute from the PHP Language extensions library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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