SlideShare a Scribd company logo
1 of 23
Download to read offline
PHP7: Scalar Type
Hints & Return Types
2015 April 1
Kansas City PHP User Group
PHP 7
It’s coming!
Q4 2015
Image by Aaron Van Noy
https://plus.google.com/+AaronVanNoy/posts/HPtSxAGcpAd
PHP 5 Type Hinting
PHP 5.1
• Objects
• Interfaces
• Array
PHP 5.4
• Callable
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));

}


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
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));

}

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
PHP 7 Scalar Type Hinting
PHP 5 Type Hinting +++ Scalars
• Strings
• Integers
• Floats
• Booleans
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
PHP7 Scalar Type Hinting
Example 1: Turned Off
<?php

declare(strict_types=0);



/**

* @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;
PHP7 Scalar Type Hinting
Example 1: Turned Off
<?php

declare(strict_types=0);



/**

* @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
221 Baker St, #B
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;
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
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
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;

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
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;

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
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;

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
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
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/
Thank You
Eric Poe

eric@ericpoe.com

@eric_poe
Please rate this talk:

https://joind.in/14348

More Related Content

What's hot

Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
What's new in PHP 5.5
What's new in PHP 5.5What's new in PHP 5.5
What's new in PHP 5.5Tom Corrigan
 
A new way to develop with WordPress!
A new way to develop with WordPress!A new way to develop with WordPress!
A new way to develop with WordPress!David Sanchez
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop NotesPamela Fox
 
PHP 5.6 New and Deprecated Features
PHP 5.6  New and Deprecated FeaturesPHP 5.6  New and Deprecated Features
PHP 5.6 New and Deprecated FeaturesMark Niebergall
 
Codeigniter4の比較と検証
Codeigniter4の比較と検証Codeigniter4の比較と検証
Codeigniter4の比較と検証ME iBotch
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life CycleXinchen Hui
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐいHisateru Tanaka
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboardsDenis Ristic
 
Meet up symfony 16 juin 2017 - Les PSR
Meet up symfony 16 juin 2017 -  Les PSRMeet up symfony 16 juin 2017 -  Les PSR
Meet up symfony 16 juin 2017 - Les PSRJulien Vinber
 
Cli the other SAPI confoo11
Cli the other SAPI confoo11Cli the other SAPI confoo11
Cli the other SAPI confoo11Combell NV
 

What's hot (20)

Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
What's new in PHP 5.5
What's new in PHP 5.5What's new in PHP 5.5
What's new in PHP 5.5
 
A new way to develop with WordPress!
A new way to develop with WordPress!A new way to develop with WordPress!
A new way to develop with WordPress!
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
PHP 5.6 New and Deprecated Features
PHP 5.6  New and Deprecated FeaturesPHP 5.6  New and Deprecated Features
PHP 5.6 New and Deprecated Features
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Codeigniter4の比較と検証
Codeigniter4の比較と検証Codeigniter4の比較と検証
Codeigniter4の比較と検証
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 
Meet up symfony 16 juin 2017 - Les PSR
Meet up symfony 16 juin 2017 -  Les PSRMeet up symfony 16 juin 2017 -  Les PSR
Meet up symfony 16 juin 2017 - Les PSR
 
Modern PHP
Modern PHPModern PHP
Modern PHP
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 
Отладка в GDB
Отладка в GDBОтладка в GDB
Отладка в GDB
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Cli the other SAPI confoo11
Cli the other SAPI confoo11Cli the other SAPI confoo11
Cli the other SAPI confoo11
 

Viewers also liked

PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodeSWIFTotter Solutions
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPantMark Baker
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsMark Baker
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tigerElizabeth Smith
 
Programming with Cmdr. Chris Hadfield
Programming with Cmdr. Chris HadfieldProgramming with Cmdr. Chris Hadfield
Programming with Cmdr. Chris HadfieldMatthias Noback
 
Enterprise Architecture Case in PHP (MUZIK Online)
Enterprise Architecture Case in PHP (MUZIK Online)Enterprise Architecture Case in PHP (MUZIK Online)
Enterprise Architecture Case in PHP (MUZIK Online)Yi-Feng Tzeng
 
Guard Authentication: Powerful, Beautiful Security
Guard Authentication: Powerful, Beautiful SecurityGuard Authentication: Powerful, Beautiful Security
Guard Authentication: Powerful, Beautiful SecurityRyan Weaver
 
Symfony: Your Next Microframework (SymfonyCon 2015)
Symfony: Your Next Microframework (SymfonyCon 2015)Symfony: Your Next Microframework (SymfonyCon 2015)
Symfony: Your Next Microframework (SymfonyCon 2015)Ryan Weaver
 
Andres Gutierrez "Phalcon 3.0, Zephir & PHP7"
Andres Gutierrez "Phalcon 3.0, Zephir & PHP7"Andres Gutierrez "Phalcon 3.0, Zephir & PHP7"
Andres Gutierrez "Phalcon 3.0, Zephir & PHP7"Fwdays
 
What's New In PHP7
What's New In PHP7What's New In PHP7
What's New In PHP7Petra Barus
 
Hexagonal architecture message-oriented software design
Hexagonal architecture   message-oriented software designHexagonal architecture   message-oriented software design
Hexagonal architecture message-oriented software designMatthias Noback
 
install PHP7 on CentOS7 by Ansible
install PHP7 on CentOS7 by Ansibleinstall PHP7 on CentOS7 by Ansible
install PHP7 on CentOS7 by AnsibleDQNEO
 

Viewers also liked (15)

Php extensions
Php extensionsPhp extensions
Php extensions
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPant
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 
What's new with PHP7
What's new with PHP7What's new with PHP7
What's new with PHP7
 
The IoC Hydra
The IoC HydraThe IoC Hydra
The IoC Hydra
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
 
Programming with Cmdr. Chris Hadfield
Programming with Cmdr. Chris HadfieldProgramming with Cmdr. Chris Hadfield
Programming with Cmdr. Chris Hadfield
 
Enterprise Architecture Case in PHP (MUZIK Online)
Enterprise Architecture Case in PHP (MUZIK Online)Enterprise Architecture Case in PHP (MUZIK Online)
Enterprise Architecture Case in PHP (MUZIK Online)
 
Guard Authentication: Powerful, Beautiful Security
Guard Authentication: Powerful, Beautiful SecurityGuard Authentication: Powerful, Beautiful Security
Guard Authentication: Powerful, Beautiful Security
 
Symfony: Your Next Microframework (SymfonyCon 2015)
Symfony: Your Next Microframework (SymfonyCon 2015)Symfony: Your Next Microframework (SymfonyCon 2015)
Symfony: Your Next Microframework (SymfonyCon 2015)
 
Andres Gutierrez "Phalcon 3.0, Zephir & PHP7"
Andres Gutierrez "Phalcon 3.0, Zephir & PHP7"Andres Gutierrez "Phalcon 3.0, Zephir & PHP7"
Andres Gutierrez "Phalcon 3.0, Zephir & PHP7"
 
What's New In PHP7
What's New In PHP7What's New In PHP7
What's New In PHP7
 
Hexagonal architecture message-oriented software design
Hexagonal architecture   message-oriented software designHexagonal architecture   message-oriented software design
Hexagonal architecture message-oriented software design
 
install PHP7 on CentOS7 by Ansible
install PHP7 on CentOS7 by Ansibleinstall PHP7 on CentOS7 by Ansible
install PHP7 on CentOS7 by Ansible
 

Similar to PHP7 - Scalar Type Hints & Return Types

Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functionsmussawir20
 
Giới thiệu PHP 7
Giới thiệu PHP 7Giới thiệu PHP 7
Giới thiệu PHP 7ZendVN
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overviewjsmith92
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolveXSolve
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Jason Lotito
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016Britta Alex
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityGeorgePeterBanyard
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Stephan Schmidt
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges✅ William Pinaud
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)Kana Natsuno
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - phpHung-yu Lin
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxShaliniPrabakaran
 

Similar to PHP7 - Scalar Type Hints & Return Types (20)

Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Giới thiệu PHP 7
Giới thiệu PHP 7Giới thiệu PHP 7
Giới thiệu PHP 7
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptx
 

More from Eric Poe

Lately in php - 2019 May 4
Lately in php - 2019 May 4Lately in php - 2019 May 4
Lately in php - 2019 May 4Eric Poe
 
2019 January - The Month in PHP
2019 January - The Month in PHP2019 January - The Month in PHP
2019 January - The Month in PHPEric Poe
 
2018 November - The Month in PHP
2018 November - The Month in PHP2018 November - The Month in PHP
2018 November - The Month in PHPEric Poe
 
2018 October - The Month in PHP
2018 October - The Month in PHP2018 October - The Month in PHP
2018 October - The Month in PHPEric Poe
 
2018 September - The Month in PHP
2018 September - The Month in PHP2018 September - The Month in PHP
2018 September - The Month in PHPEric Poe
 
2018 July - The Month in PHP
2018 July - The Month in PHP2018 July - The Month in PHP
2018 July - The Month in PHPEric Poe
 
Last Month in PHP - May 2018
Last Month in PHP - May 2018Last Month in PHP - May 2018
Last Month in PHP - May 2018Eric Poe
 
Composer yourself: a reintroduction to composer
Composer yourself:  a reintroduction to composerComposer yourself:  a reintroduction to composer
Composer yourself: a reintroduction to composerEric Poe
 
Last Month in PHP - April 2018
Last Month in PHP - April 2018Last Month in PHP - April 2018
Last Month in PHP - April 2018Eric Poe
 
Last Month in PHP - March 2018
Last Month in PHP - March 2018Last Month in PHP - March 2018
Last Month in PHP - March 2018Eric Poe
 
Last 2 Months in PHP - January 2018
Last 2 Months in PHP - January 2018Last 2 Months in PHP - January 2018
Last 2 Months in PHP - January 2018Eric Poe
 
Last Month in PHP - June through Mid-July 2017
Last Month in PHP - June through Mid-July 2017Last Month in PHP - June through Mid-July 2017
Last Month in PHP - June through Mid-July 2017Eric Poe
 
Last Month in PHP - April 2017
Last Month in PHP - April 2017Last Month in PHP - April 2017
Last Month in PHP - April 2017Eric Poe
 
Last Month in PHP - March 2017
Last Month in PHP - March 2017Last Month in PHP - March 2017
Last Month in PHP - March 2017Eric Poe
 
Last Month in PHP - February 2017
Last Month in PHP - February 2017Last Month in PHP - February 2017
Last Month in PHP - February 2017Eric Poe
 
Last Month in PHP - December 2016
Last Month in PHP - December 2016Last Month in PHP - December 2016
Last Month in PHP - December 2016Eric Poe
 
Last Month in PHP - November 2016
Last Month in PHP - November 2016Last Month in PHP - November 2016
Last Month in PHP - November 2016Eric Poe
 
Last Month in PHP - October 2016
Last Month in PHP - October 2016Last Month in PHP - October 2016
Last Month in PHP - October 2016Eric Poe
 
Last Month in PHP - September 2016
Last Month in PHP - September 2016Last Month in PHP - September 2016
Last Month in PHP - September 2016Eric Poe
 
Last 2 Months in PHP - July & August 2016
Last 2 Months in PHP - July & August 2016Last 2 Months in PHP - July & August 2016
Last 2 Months in PHP - July & August 2016Eric Poe
 

More from Eric Poe (20)

Lately in php - 2019 May 4
Lately in php - 2019 May 4Lately in php - 2019 May 4
Lately in php - 2019 May 4
 
2019 January - The Month in PHP
2019 January - The Month in PHP2019 January - The Month in PHP
2019 January - The Month in PHP
 
2018 November - The Month in PHP
2018 November - The Month in PHP2018 November - The Month in PHP
2018 November - The Month in PHP
 
2018 October - The Month in PHP
2018 October - The Month in PHP2018 October - The Month in PHP
2018 October - The Month in PHP
 
2018 September - The Month in PHP
2018 September - The Month in PHP2018 September - The Month in PHP
2018 September - The Month in PHP
 
2018 July - The Month in PHP
2018 July - The Month in PHP2018 July - The Month in PHP
2018 July - The Month in PHP
 
Last Month in PHP - May 2018
Last Month in PHP - May 2018Last Month in PHP - May 2018
Last Month in PHP - May 2018
 
Composer yourself: a reintroduction to composer
Composer yourself:  a reintroduction to composerComposer yourself:  a reintroduction to composer
Composer yourself: a reintroduction to composer
 
Last Month in PHP - April 2018
Last Month in PHP - April 2018Last Month in PHP - April 2018
Last Month in PHP - April 2018
 
Last Month in PHP - March 2018
Last Month in PHP - March 2018Last Month in PHP - March 2018
Last Month in PHP - March 2018
 
Last 2 Months in PHP - January 2018
Last 2 Months in PHP - January 2018Last 2 Months in PHP - January 2018
Last 2 Months in PHP - January 2018
 
Last Month in PHP - June through Mid-July 2017
Last Month in PHP - June through Mid-July 2017Last Month in PHP - June through Mid-July 2017
Last Month in PHP - June through Mid-July 2017
 
Last Month in PHP - April 2017
Last Month in PHP - April 2017Last Month in PHP - April 2017
Last Month in PHP - April 2017
 
Last Month in PHP - March 2017
Last Month in PHP - March 2017Last Month in PHP - March 2017
Last Month in PHP - March 2017
 
Last Month in PHP - February 2017
Last Month in PHP - February 2017Last Month in PHP - February 2017
Last Month in PHP - February 2017
 
Last Month in PHP - December 2016
Last Month in PHP - December 2016Last Month in PHP - December 2016
Last Month in PHP - December 2016
 
Last Month in PHP - November 2016
Last Month in PHP - November 2016Last Month in PHP - November 2016
Last Month in PHP - November 2016
 
Last Month in PHP - October 2016
Last Month in PHP - October 2016Last Month in PHP - October 2016
Last Month in PHP - October 2016
 
Last Month in PHP - September 2016
Last Month in PHP - September 2016Last Month in PHP - September 2016
Last Month in PHP - September 2016
 
Last 2 Months in PHP - July & August 2016
Last 2 Months in PHP - July & August 2016Last 2 Months in PHP - July & August 2016
Last 2 Months in PHP - July & August 2016
 

Recently uploaded

Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 

Recently uploaded (20)

Odoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting ServiceOdoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting Service
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 

PHP7 - Scalar Type Hints & Return Types

  • 1. PHP7: Scalar Type Hints & Return Types 2015 April 1 Kansas City PHP User Group
  • 2. PHP 7 It’s coming! Q4 2015 Image by Aaron Van Noy https://plus.google.com/+AaronVanNoy/posts/HPtSxAGcpAd
  • 3. PHP 5 Type Hinting PHP 5.1 • Objects • Interfaces • Array PHP 5.4 • Callable
  • 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
  • 10. PHP7 Scalar Type Hinting Example 1: Turned Off <?php
 declare(strict_types=0);
 
 /**
 * @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;
  • 11. PHP7 Scalar Type Hinting Example 1: Turned Off <?php
 declare(strict_types=0);
 
 /**
 * @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 221 Baker St, #B
  • 12. 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;
  • 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/
  • 23. Thank You Eric Poe
 eric@ericpoe.com
 @eric_poe Please rate this talk:
 https://joind.in/14348