Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deshack/9a8ad29b7b6963b5fb896401b1bfe4ea to your computer and use it in GitHub Desktop.
Save deshack/9a8ad29b7b6963b5fb896401b1bfe4ea to your computer and use it in GitHub Desktop.
PHP7 Return Type Declarations with interfaces
<?php
class User {}
interface UserFactoryContract {
public static function generate() : User;
}
class UserFactory implements UserFactoryContract {
public static function generate() {
// PHP Fatal error: Declaration of UserFactory::generate() must be compatible with UserFactoryContract::generate(): User
}
public static function generate() : User {
return [];
// PHP Warning: Uncaught TypeError: Return value of UserFactory::getUserWrong() must be an instance of User
}
public static function generate() : User {
return new User;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment