Created
June 6, 2016 15:55
-
-
Save deshack/9a8ad29b7b6963b5fb896401b1bfe4ea to your computer and use it in GitHub Desktop.
PHP7 Return Type Declarations with interfaces
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
<?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