Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User
- {
- public function __construct(
- private int $id = 0,
- private string $name = ''
- )
- {
- }
- public static function getInstance($id, $name) : self
- {
- return new self($id, $name);
- }
- public function getUserName() : string
- {
- return $this->name;
- }
- }
- class SomeClass
- {
- private User $user;
- public function __construct(
- private User $user = User::getInstance($id, $name)
- )
- {
- //$this->user = $user::getInstance()
- }
- public function getUserName(): string
- {
- return $this->user->getUserName();
- }
- }
- $user = new User(1, 'John');
- $cls = new SomeClass($user);
- echo $cls->getId();
Add Comment
Please, Sign In to add comment