-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD examples of Friend with inheritance
- Loading branch information
1 parent
a360857
commit 9fe0020
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendWithInheritance; | ||
|
||
use DaveLiddament\PhpLanguageExtensions\Friend; | ||
|
||
|
||
interface Emailer | ||
{ | ||
#[Friend(EmailQueueProcessor::class)] | ||
public function sendEmail(): void; | ||
} | ||
|
||
class PhpMailer implements Emailer | ||
{ | ||
public function sendEmail(): void | ||
{ | ||
} | ||
} | ||
|
||
|
||
class EmailQueueProcessor | ||
{ | ||
public function sendEmail(): void | ||
{ | ||
$mailer = new PhpMailer(); | ||
$mailer->sendEmail(); // OK | ||
} | ||
} | ||
|
||
class AnotherClass { | ||
public function sendEmail(): void | ||
{ | ||
$mailer = new PhpMailer(); | ||
$mailer->sendEmail(); // ERROR | ||
} | ||
} | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendWithInheritance2; | ||
|
||
use DaveLiddament\PhpLanguageExtensions\Friend; | ||
|
||
|
||
interface Command | ||
{ | ||
#[Friend(CommandProcessor::class)] | ||
public function execute(): void; | ||
} | ||
|
||
class PrintCommand implements Command | ||
{ | ||
|
||
public function execute(): void | ||
{ | ||
} | ||
} | ||
|
||
|
||
interface CommandProcessor | ||
{ | ||
public function process(Command $command); | ||
} | ||
|
||
class PrintCommandProcessor implements CommandProcessor | ||
{ | ||
public function process(Command $command): void | ||
{ | ||
$command->execute(); // OK | ||
} | ||
} | ||
|
||
|
||
class AnotherClass | ||
{ | ||
public function process(Command $command): void | ||
{ | ||
$command->execute(); // ERROR | ||
} | ||
} |