SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado

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
 

Destacado (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 a 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 a 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 Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
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
 

Más de 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
 

Más de 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
 

Último

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 

Último (20)

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 

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