Created
March 28, 2025 11:22
-
-
Save adamcameron/82f22081b00a6d215da42687161868be to your computer and use it in GitHub Desktop.
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
<?php | |
class Tester { | |
function testUser(User $user): void { | |
$user->returnsUserInterface(); | |
$user->returnsUser(); | |
} | |
function testUserInterface(UserInterface $user): void { | |
$user->returnsUserInterface(); | |
$user->returnsUser(); // to me, this should error-out | |
} | |
} | |
class Factory { | |
public function getUserInterface(): UserInterface { | |
return new User(); | |
} | |
} | |
class User implements UserInterface { | |
public function returnsUserInterface(): UserInterface { | |
echo 'Returning as UserInterface' . PHP_EOL; | |
return $this; | |
} | |
public function returnsUser(): User { | |
echo 'Returning as User' . PHP_EOL; | |
return $this; | |
} | |
} | |
interface UserInterface { | |
public function returnsUserInterface(): UserInterface; | |
} | |
$tester = new Tester(); | |
$factory = new Factory(); | |
$sut = $factory->getUserInterface(); | |
$tester->testUser($sut); | |
echo PHP_EOL; | |
$tester->testUserInterface($sut); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment