Skip to content

Commit

Permalink
ADD examples of Friend with inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveLiddament committed Aug 12, 2024
1 parent a360857 commit 9fe0020
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/friend/friendWIthInheritance.php
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
}
}


45 changes: 45 additions & 0 deletions examples/friend/friendWIthInheritance2.php
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
}
}

0 comments on commit 9fe0020

Please sign in to comment.