Created
June 6, 2016 15:38
-
-
Save deshack/df2393f94891590c31ce1c4d3f34fee9 to your computer and use it in GitHub Desktop.
PHP7 Return Type Declarations
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 | |
/** | |
* Return type in PHP5. | |
* | |
* @copyright 2016 SqueezyWeb | |
* @author Mattia Migliorini <[email protected]> | |
*/ | |
/** | |
* Get User instance. | |
*/ | |
function getUser() { | |
// Let's return something else... | |
return array(); | |
} | |
var_dump(getUser()); | |
// Output: | |
// array(0) { | |
// } |
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 | |
/** | |
* PHP7 Return Type Declarations with interfaces. | |
* | |
* @copyright 2016 SqueezyWeb | |
* @author Mattia Migliorini <[email protected]> | |
*/ | |
/** | |
* User class. | |
*/ | |
class User { | |
} | |
/** | |
* User factory interface. | |
*/ | |
interface UserFactoryContract { | |
/** | |
* Generate a new User object. | |
* | |
* Uses Return Type Declaration to establish a strict contract. | |
* | |
* @return User | |
*/ | |
public static function generate() : User; | |
} | |
/** | |
* User Factory. | |
*/ | |
class UserFactory implements UserFactoryContract { | |
/** | |
* What if we don't declare the return type in the implementation? | |
*/ | |
public static function generate() { | |
// This method will raise the following fatal error at compile time: | |
// PHP Fatal error: Declaration of UserFactory::generate() must be compatible with UserFactoryContract::generate(): User | |
} | |
/** | |
* Let's do it right...or maybe not! | |
*/ | |
public static function generate() : User { | |
return []; | |
// Returns a PHP warning at runtime: | |
// PHP Warning: Uncaught TypeError: Return value of UserFactory::getUserWrong() must be an instance of User | |
} | |
/** | |
* This time it's right! | |
*/ | |
public static function generate() : User { | |
return new User; | |
} | |
} |
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 | |
/** | |
* Return type declaration in PHP7. | |
* | |
* @copyright 2016 SqueezyWeb | |
* @author Mattia Migliorini <[email protected]> | |
*/ | |
/** | |
* User class. | |
*/ | |
class User { | |
} | |
/** | |
* Get User without return type declaration. | |
*/ | |
function maybeGetUser() { | |
return []; | |
} | |
var_dump(maybeGetUser()); | |
// Output: | |
// array(0) { | |
// } | |
// And we're ok with this, we can still do it. | |
/** | |
* Get user with return type declaration and wrong return type. | |
*/ | |
function getUserWrong() : User { | |
return []; | |
} | |
getUserWrong(); | |
// Result: | |
// PHP Warning: Uncaught TypeError: Return value of getUserWrong() must be an instance of User, array returned | |
/** | |
* Get User with return type declaration. | |
* | |
* This time it's ok! | |
*/ | |
function getUser() : User { | |
return new User; | |
} | |
var_dump(getUser()); | |
// Output: | |
// object(User)#1 (0) { | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment