Skip to content

Instantly share code, notes, and snippets.

@adamcameron
Created March 28, 2025 11:22
Show Gist options
  • Save adamcameron/82f22081b00a6d215da42687161868be to your computer and use it in GitHub Desktop.
Save adamcameron/82f22081b00a6d215da42687161868be to your computer and use it in GitHub Desktop.
<?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