SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar 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.5
Tom Corrigan
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
Xinchen Hui
 

Tendances (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
 

En vedette

En vedette (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
 

Similaire à 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 Functions
mussawir20
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 

Similaire à 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
 

Plus de Eric Poe

Plus 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
 

Dernier

%+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
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+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
 

Dernier (20)

The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%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
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
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
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%+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...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%+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...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

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