Type hints were introduced with PHP 5. PHP 7 will add the ability to type hint on scalars and even to explicitly state what type will be returned from the function. This talk briefly goes over these added features to PHP 7.
4. PHP5 Type Hinting Example
<?php
/**
* Type Hinting in PHP 5 is only for classes,
* interfaces, & callable
*
* @param DateTime $timestamp
* @return string
*/
function getDayOfWeek(DateTime $timestamp)
{
return $timestamp->format('l');
}
$times = [];
$times[] = new DateTime('now');
$times[] = new DateTimeImmutable('+3 days');
foreach($times as $time) {
printf("Today is %sn",
getDayOfWeek($time));
}
5. PHP5 Type Hinting Example
<?php
/**
* Type Hinting in PHP 5 is only for classes,
* interfaces, & callable
*
* @param DateTime $timestamp
* @return string
*/
function getDayOfWeek(DateTime $timestamp)
{
return $timestamp->format('l');
}
$times = [];
$times[] = new DateTime('now');
$times[] = new DateTimeImmutable('+3 days');
foreach($times as $time) {
printf("Today is %sn",
getDayOfWeek($time));
}
Today is Thursday
Fatal error: Argument 1 passed to
getDayOfWeek() must be an instance of
DateTime, instance of DateTimeImmutable
given, called in /vagrant_data/
php5TypeHint.php on line 19 and defined in /
vagrant_data/php5TypeHint.php on line 9
6. PHP5 Type Hinting Example
<?php
/**
* Type Hinting in PHP 5 is only for classes,
* interfaces, & callable
*
* @param DateTimeInterface $timestamp
* @return string
*/
function getDayOfWeek(DateTimeInterface
$timestamp)
{
return $timestamp->format('l');
}
$times = [];
$times[] = new DateTime('now');
$times[] = new DateTimeImmutable('+3 days');
foreach($times as $time) {
printf("Today is %sn",
getDayOfWeek($time));
}
7. PHP5 Type Hinting Example
<?php
/**
* Type Hinting in PHP 5 is only for classes,
* interfaces, & callable
*
* @param DateTimeInterface $timestamp
* @return string
*/
function getDayOfWeek(DateTimeInterface
$timestamp)
{
return $timestamp->format('l');
}
$times = [];
$times[] = new DateTime('now');
$times[] = new DateTimeImmutable('+3 days');
foreach($times as $time) {
printf("Today is %sn",
getDayOfWeek($time));
}
Today is Thursday
Today is Sunday
8. PHP 7 Scalar Type Hinting
PHP 5 Type Hinting +++ Scalars
• Strings
• Integers
• Floats
• Booleans
9. Scalar Type Hinting
• Not turned on by default
• Turn on by making `declare(strict_types=1);` the
first statement in your file
• Only strict on the file with the function call
13. PHP7 Scalar Type Hinting
Example 1: Turned On
<?php
declare(strict_types=1);
/**
* @param int $number
* @param string $street
* @param string $apt
* @return string
*/
function createStreetAddress(int $number,
string $street, string $apt = null)
{
if ($apt) {
return sprintf('%d %s, #%s', $number,
$street, $apt);
} else {
return sprintf('%d %s', $number,
$street);
}
}
echo createStreetAddress(221, "Baker St",
"B") . PHP_EOL;
echo createStreetAddress("221", "Baker St",
"B") . PHP_EOL;
221 Baker St, #B
Fatal error: Argument 1 passed to
createStreetAddress() must be of the type
integer, string given, called in /
vagrant_data/php7TypeHint.php on line 20 and
defined in /vagrant_data/php7TypeHint.php on
line 10
14. Introducing… return types
• Completely optional
• Declare strict same as for Scalar Type Hinting
• Only strict on the file with the function
declaration
• Tells the compiler that we expect to get
something of type Foo out of the function call
15. PHP 7 Return Types
Example 1: DateTime
<?php
declare(strict_types=1);
/**
* @return DateTime
*/
function getCurrentTime() : DateTime {
return new DateTime('now');
}
echo getCurrentTime()->format('l') . PHP_EOL;
16. PHP 7 Return Types
Example 1: DateTime
<?php
declare(strict_types=1);
/**
* @return DateTime
*/
function getCurrentTime() : DateTime {
return new DateTime('now');
}
echo getCurrentTime()->format('l') . PHP_EOL;
Thursday
17. PHP 7 Return Types
Example 1: Calculator
<?php
declare(strict_types=1);
/**
* @param int $num
* @param int $denom
* @return int
*/
function divide(int $num, int $denom) : int {
if (0 === $denom) {
return 9999999999;
} else {
return $num / $denom;
}
}
echo divide(7, 3) . PHP_EOL;
18. PHP 7 Return Types
Example 1: Calculator
<?php
declare(strict_types=1);
/**
* @param int $num
* @param int $denom
* @return int
*/
function divide(int $num, int $denom) : int {
if (0 === $denom) {
return 9999999999;
} else {
return $num / $denom;
}
}
echo divide(7, 3) . PHP_EOL;
Fatal error: Return value of divide() must be
of the type integer, float returned in /
vagrant_data/php7ReturnType.php on line 13 in
/vagrant_data/php7ReturnType.php on line 13
19. PHP 7 Return Types
Example 1: Calculator
<?php
declare(strict_types=1);
/**
* @param int $num
* @param int $denom
* @return float
*/
function divide(int $num, int $denom) : float
{
if (0 === $denom) {
return 9999999999;
} else {
return $num / $denom;
}
}
echo divide(7, 3) . PHP_EOL;
20. PHP 7 Return Types
Example 1: Calculator
<?php
declare(strict_types=1);
/**
* @param int $num
* @param int $denom
* @return float
*/
function divide(int $num, int $denom) : float
{
if (0 === $denom) {
return 9999999999;
} else {
return $num / $denom;
}
}
echo divide(7, 3) . PHP_EOL;
2.3333333333333
21. Great. PHP Just Got Hard.
Again.
• No. These are optional
• Weak Typing is still the default
• Strict typing forces the programmer to think more
clearly about a function’s input/output
• Think: “Filter input; escape output.”
• Leads the way to compiling PHP to Opcode
• Compiler can catch certain bugs before a user will
22. More Information
• Scalar Type Hinting & Return Types - RFC:
https://wiki.php.net/rfc/scalar_type_hints_v5
• Anthony Ferrara’s blog post about this:
http://blog.ircmaxell.com/2015/02/scalar-types-
and-php.html
• Building & Testing PHP 7:
http://akrabat.com/building-and-testing-php7/