SlideShare a Scribd company logo
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

Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
Bradley Holt
 
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 Notes
Pamela 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 Features
Mark Niebergall
 
Php mysql
Php mysqlPhp mysql
Php mysql
Alebachew Zewdu
 
Codeigniter4の比較と検証
Codeigniter4の比較と検証Codeigniter4の比較と検証
Codeigniter4の比較と検証
ME iBotch
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life CycleXinchen Hui
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
Nidhi mishra
 
関西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 #burningkeyboards
Denis 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 PSR
Julien Vinber
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
Santhiya Grace
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
Mark Niebergall
 
Cli the other SAPI confoo11
Cli the other SAPI confoo11Cli the other SAPI confoo11
Cli the other SAPI confoo11
Combell 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 extensions
Php extensionsPhp extensions
Php extensions
Elizabeth Smith
 
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
SWIFTotter Solutions
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPant
Mark 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 extensions
Mark Baker
 
What's new with PHP7
What's new with PHP7What's new with PHP7
What's new with PHP7
SWIFTotter Solutions
 
The IoC Hydra
The IoC HydraThe IoC Hydra
The IoC Hydra
Kacper Gunia
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
Elizabeth Smith
 
Programming with Cmdr. Chris Hadfield
Programming with Cmdr. Chris HadfieldProgramming with Cmdr. Chris Hadfield
Programming with Cmdr. Chris Hadfield
Matthias 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 Security
Ryan 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 PHP7
Petra Barus
 
Hexagonal architecture message-oriented software design
Hexagonal architecture   message-oriented software designHexagonal architecture   message-oriented software design
Hexagonal architecture message-oriented software design
Matthias Noback
 
install PHP7 on CentOS7 by Ansible
install PHP7 on CentOS7 by Ansibleinstall PHP7 on CentOS7 by Ansible
install PHP7 on CentOS7 by Ansible
DQNEO
 

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

Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
Aftabali702240
 
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 7
ZendVN
 
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 / XSolve
XSolve
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13
Jason 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 2016
Britta Alex
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
Marcello Duarte
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
GeorgePeterBanyard
 
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 5
Stephan Schmidt
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
Vineet Kumar Saini
 
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 developers
Stoyan Stefanov
 
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)
PHP「参照渡しできるよ」(君の考えている参照渡しと同じとは言ってない)
Kana Natsuno
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
Marcello Duarte
 
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.pptx
ShaliniPrabakaran
 

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 4
Eric Poe
 
2019 January - The Month in PHP
2019 January - The Month in PHP2019 January - The Month in PHP
2019 January - The Month in PHP
Eric Poe
 
2018 November - The Month in PHP
2018 November - The Month in PHP2018 November - The Month in PHP
2018 November - The Month in PHP
Eric Poe
 
2018 October - The Month in PHP
2018 October - The Month in PHP2018 October - The Month in PHP
2018 October - The Month in PHP
Eric Poe
 
2018 September - The Month in PHP
2018 September - The Month in PHP2018 September - The Month in PHP
2018 September - The Month in PHP
Eric Poe
 
2018 July - The Month in PHP
2018 July - The Month in PHP2018 July - The Month in PHP
2018 July - The Month in PHP
Eric Poe
 
Last Month in PHP - May 2018
Last Month in PHP - May 2018Last Month in PHP - May 2018
Last Month in PHP - May 2018
Eric Poe
 
Composer yourself: a reintroduction to composer
Composer yourself:  a reintroduction to composerComposer yourself:  a reintroduction to composer
Composer yourself: a reintroduction to composer
Eric Poe
 
Last Month in PHP - April 2018
Last Month in PHP - April 2018Last Month in PHP - April 2018
Last Month in PHP - April 2018
Eric Poe
 
Last Month in PHP - March 2018
Last Month in PHP - March 2018Last Month in PHP - March 2018
Last Month in PHP - March 2018
Eric 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 2018
Eric 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 2017
Eric Poe
 
Last Month in PHP - April 2017
Last Month in PHP - April 2017Last Month in PHP - April 2017
Last Month in PHP - April 2017
Eric Poe
 
Last Month in PHP - March 2017
Last Month in PHP - March 2017Last Month in PHP - March 2017
Last Month in PHP - March 2017
Eric Poe
 
Last Month in PHP - February 2017
Last Month in PHP - February 2017Last Month in PHP - February 2017
Last Month in PHP - February 2017
Eric Poe
 
Last Month in PHP - December 2016
Last Month in PHP - December 2016Last Month in PHP - December 2016
Last Month in PHP - December 2016
Eric Poe
 
Last Month in PHP - November 2016
Last Month in PHP - November 2016Last Month in PHP - November 2016
Last Month in PHP - November 2016
Eric Poe
 
Last Month in PHP - October 2016
Last Month in PHP - October 2016Last Month in PHP - October 2016
Last Month in PHP - October 2016
Eric Poe
 
Last Month in PHP - September 2016
Last Month in PHP - September 2016Last Month in PHP - September 2016
Last Month in PHP - September 2016
Eric 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 2016
Eric 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

Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 

Recently uploaded (20)

Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 

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