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() { |
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 {} | |
function getUserWrong() : User { | |
return []; | |
} | |
getUserWrong(); | |
// PHP Warning: Uncaught TypeError: Return value of getUserWrong() must be an instance of User, array returned |
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 | |
function maybeGetUser() { | |
return []; | |
} | |
var_dump(maybeGetUser()); | |
// array(0) { | |
// } |
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 | |
function getUser() { | |
return array(); | |
} | |
var_dump(getUser()); | |
// array(0) { | |
// } |
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 | |
/** | |
* Return type in PHP5. | |
* | |
* @copyright 2016 SqueezyWeb | |
* @author Mattia Migliorini <[email protected]> | |
*/ | |
/** | |
* Get User instance. |
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 | |
// Force Type Hinting to do a strict type check (the same as ===). | |
declare(strict_types=1); | |
function setBool(bool $bool) { | |
var_dump($bool); | |
} | |
setBool('foo'); |
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 | |
function setBool(bool $bool) { | |
var_dump($bool); | |
} | |
setBool(true); // bool(true) | |
setBool('foo'); // bool(true) | |
setBool(''); // bool(false) |
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 | |
function setValue(string $value) { | |
var_dump($value); | |
} | |
setValue('foo'); | |
// string(3) "foo" | |
setValue([]); |
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 | |
function setOptions(array $options) { | |
var_dump($options); | |
} | |
setOptions(array()); | |
// array(0) { | |
// } |
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 | |
/** | |
* PHP5 Type Hinting Example. | |
* | |
* @copyright 2016 SqueezyWeb | |
* @author Mattia Migliorini <[email protected]> | |
*/ | |
/** | |
* Array Type Hinting. |
NewerOlder