Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View deshack's full-sized avatar

Mattia Migliorini deshack

View GitHub Profile
@deshack
deshack / php7-return-type-declarations-interface.php
Created June 6, 2016 15:55
PHP7 Return Type Declarations with interfaces
<?php
class User {}
interface UserFactoryContract {
public static function generate() : User;
}
class UserFactory implements UserFactoryContract {
public static function generate() {
@deshack
deshack / php7-return-type-declarations-usage.php
Created June 6, 2016 15:47
PHP7 usage of return type declarations
<?php
class User {}
function getUserWrong() : User {
return [];
}
getUserWrong();
// PHP Warning: Uncaught TypeError: Return value of getUserWrong() must be an instance of User, array returned
@deshack
deshack / php7-dynamic-return-type.php
Created June 6, 2016 15:44
PHP7 dynamic return type
<?php
function maybeGetUser() {
return [];
}
var_dump(maybeGetUser());
// array(0) {
// }
@deshack
deshack / php5-return-types.php
Created June 6, 2016 15:41
PHP5 Return Types
<?php
function getUser() {
return array();
}
var_dump(getUser());
// array(0) {
// }
@deshack
deshack / php5-return-type.php
Created June 6, 2016 15:38
PHP7 Return Type Declarations
<?php
/**
* Return type in PHP5.
*
* @copyright 2016 SqueezyWeb
* @author Mattia Migliorini <mattia@squeezyweb.com>
*/
/**
* Get User instance.
@deshack
deshack / php7-type-hinting-strict.php
Created May 27, 2016 14:30
PHP7 Strict Type Hinting - see https://gist.github.com/deshack/e63358a8726b7c3e0ba13563e4f9864c for a full collection of examples about PHP5/PHP7 type hinting
<?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');
@deshack
deshack / php7-type-hinting-casting.php
Last active May 27, 2016 14:40
PHP7 Type Hinting with Casting - see https://gist.github.com/deshack/e63358a8726b7c3e0ba13563e4f9864c for a full collection of examples about PHP5/PHP7 type hinting
<?php
function setBool(bool $bool) {
var_dump($bool);
}
setBool(true); // bool(true)
setBool('foo'); // bool(true)
setBool(''); // bool(false)
@deshack
deshack / php7-type-hinting.php
Last active May 27, 2016 14:17
PHP7 Type Hinting - see https://gist.github.com/deshack/e63358a8726b7c3e0ba13563e4f9864c for a full collection of examples about PHP5/PHP7 type hinting
<?php
function setValue(string $value) {
var_dump($value);
}
setValue('foo');
// string(3) "foo"
setValue([]);
@deshack
deshack / php5-type-hinting.php
Last active May 27, 2016 14:19
PHP5 Type Hinting - see https://gist.github.com/deshack/e63358a8726b7c3e0ba13563e4f9864c for a full collection of examples about PHP5/PHP7 type hinting
<?php
function setOptions(array $options) {
var_dump($options);
}
setOptions(array());
// array(0) {
// }
@deshack
deshack / php5-type-hinting.php
Created May 27, 2016 14:10
PHP7 Scalar Type Hinting
<?php
/**
* PHP5 Type Hinting Example.
*
* @copyright 2016 SqueezyWeb
* @author Mattia Migliorini <mattia@squeezyweb.com>
*/
/**
* Array Type Hinting.