SlideShare a Scribd company logo
PHP Leeds, Wales, United Kingdom
Strong typing in PHP
adoption, evolution and organisation
AGENDA
• PHP Type hints system
• Adoption
• Evolution
• Organisation
SPEAKER
• Damien Seguy
• CTO @exakat
• Static analysis expert
• Retirement home for elePHPants
Everyone uses typehinting
97%
3%
With Typehints Without typehint
No
typeh
ints
Adoption Hard
part
A
d
or
at
or
s
Everyone uses typehinting
Adoption
Typehints as debug tactics
• Set up a typehint
• Wait for the 'Fatal error'
• Fix the calling code, now that you know where it is
• Remove it in production
• Leave it in development
<?php
foo("a");
foo(1);
foo(new x);
foo(array());
function foo(string $s) {
echo $s;
}
class x {
function __toString()
{ return "a"; }
}
?>
Your code is already typed
• Typehints are already used in the code
• PHP, and strict_types
• is_string(), is_array(), instanceof…
• Coding conventions
Coding conventions
• The type is in the name of the argument
• $string, $array
• $user = new User();
<?php
function shorten($string) {
return substr($string, 0, 5);
}
function getPattern(array $ints) { }
function (StringClass $string) { }
?>
Moving type checks to the signature
• is_a(), is_subclass_of(), instanceof, ===
null
• is_string(), is_array()
• (int), (string)…
<?php
function bar($user) {
if ($user === null) {
throw new TypeError();
}
if (!$user instanceof User)) {
throw new TypeError();
}
return $user->validate();
}
?>
Moving type checks to the signature
• is_a(), is_subclass_of(), instanceof, ===
null
• is_string(), is_array()
• (int), (string)…
<?php
function bar(Usager $user) {
return $user->validate();
}
?>
Moving type checks to the signature
• is_a(), is_subclass_of(), instanceof, ===
null
• is_string(), is_array()
• (int), (string)…
<?php
function bar($price) {
return floor((float) $price) * 1.21;
}
?>
Follow the lead
<?php
function bar($argument) {
return substr($argument, 0, 10);
}
?>
Follow the lead
<?php
function bar(string $argument) {
return substr($argument, 0, 10);
}
?>
Follow the lead
<?php
function bar($argument) : string {
return substr($argument, 0, 10);
}
?>
Follow the lead
<?php
function bar(string $argument) : string {
return substr($argument, 0, 10) ?: '';
}
?>
Substr() returns string OR false
Strong typing : adoption, adaptation and organisation
Backward with the return types
<?php
function bar($argument) {
return foo($argument);
}
function foo($x) {
return barbar($x);
}
function barbar($y) : string {
//...
}
?>
Forward with caution
<?php
function bar3($argument) {
return foo(null);
}
?>
<?php
function bar($argument) {
return foo($argument);
}
function foo($x) {
return barbar($x);
}
function barbar(string $y) {
//...
}
?>
<?php
function bar2($argument) {
return foo(22);
}
?>
Wait for PHP 8.0
<?php
function foo($x) {
if (rand(1, 3)) {
return 1;
} else {
return barbar($x);
}
}
function barbar($y) : string {
//...
}
?>
Adoption
• Use it for debugging purposes
• Follow the types that are already there
• PHP
• Your framework
• Propagate your own types
• Return types are easy, argument types need caution
Evolution
Evolution
• Typehints now have impact on your code
• Wait for PHP 8.0 union types
• Single type is a constraint and a tool
Impact of defaults
<?php
function foo($x = -1) {
if ($x === -1) {
return '';
} else {
return substr($x, 0, 1);
}
}
?>
Impact of defaults
<?php
class foo {
const DEFAULT = -1;
private $bar = self::DEFAULT;
public function foo() {
if ($this->bar === DEFAULT) {
return '';
} else {
return substr($this->bar, 0, 1);
}
}
?>
Ambiguous cases
<?php
function foo($b) {
return array_fill(0, 10, $b);
}
function bar($a) {
return shell_exec($a);
}
?>
Ambiguous cases
<?php
declare(strict_types = 1);
function foo(array $a) { }
foo(['a', 'b', 'c', 'd' ]);
foo(['a', 'b', 'c', null, 2]);
?>
• array()
• No generic in PHP
• Use attributes and
annotations
Dubious cases
<?php
declare(strict_types = 1);
class x {
function __set($name, $a) {
//...
}
}
$x = (new x);
$x->c = 3;
?>
Fossilisation stage
• Method signatures
• Arguments count
• Compulsory versus optional
• Type
• Default values
• Visibility
Fossilisation stage
• Modify M1() in C3
• Modify M1() in C1
• Modify M1() in C2 and C4
• Modify M1() in C5
• Modify M1() in C6
• …
Evolution
no
type
scalar types class
types
non-null
interfaces
Evolution
0%
18%
35%
53%
70%
88%
object iterable callable int bool void string array scalar Classes
Evolution
• Scalar types cannot evolve
• Classes can evolve
• Class types don't need to be complex
• A price is not an int/float
• A path may be a string while a string is not a path
Evolution
no
type
scalar types
class
types
non-null
interfaces
No types
Evolution
Also no types
Evolution
Evolution
• Type hints reduces the number of possible paths in
the code
• Scalar are versatile
• They have a lot of connectivity
• Upgrading scalar types to class types reduce
complexity even more
Scalar types to classes
Evolution
Organisation
Organisation
• Typehinting has impact on the signature of a method
• This impact spreads all over the application
• It introduces a hierarchy of the classes
Classes order
Organisation
• A method returns one type
• A method has dependencies on
its arguments's type
• That object must be created
first
• A method has dependencies on
its constructor too
Classes order
Organisation
• A method returns one type
• A method has dependencies on
its arguments's type
• That object must be created
first
• A method has dependencies on
its constructor too
Classes order
Organisation
• Mysql (C1)
• Host/Login/Pwd (C2)
• Prepare (M1)
• Query (C3)
Classes order
Organisation
• Mysql (C1)
• Host/Login/Pwd (C2)
• Prepare (M1)
• Query (C3)
Classes order
Organisation
• Topological sorting of the classes
• One class has priority over another class when it is used as typehint
• There is no garantee for a single top class :
• Probably several top classes
• With scalar as argument, because they are so easy to tweak
Classes order
Organisation
Classes order
Organisation
Subsidiary questions
Organisation
• Why are there multiple methods with the same typehints?
• Potential double work
• How testable are the classes?
• How many objects do you need to create an instance?
• Can a method reach a specific type of class?
• The needed types may not be available
Adding typehints to your code base
• Adoption
• Follow the current code
• Evolution
• Refactor the code, use classes as typehints
• Organisation
• Use typehints to reduce complexity and plan evolutions
Subsidiary questions
Static analysis helps you type
• Exakat
• Typehint suggestion report, type inconsistencies and typehint report
• Phpdoctor
• Reports missing types
• Psalm
• Infers typehints on the fly
Subsidiary questions
Static analysis helps you type
• Exakat
• Typehint suggestion report, type inconsistencies and typehint report
• Phpdoctor
• Reports missing types
• Psalm
• Infers typehints on the fly
https://www.exakat.io/ | @exakat
O, diolch yn
fawr iawn
Classes order
Organisation
• M1 needs a property
• M2 provides that property
• M2 > M1()
• True in PHP 7.4+
• x::$a must not be accessed before
initialization
• True in PHP 7.0
• Call to a member function bar() on
null

More Related Content

PDF
Strong typing @ php leeds
Damien Seguy
 
PPT
Functional OOP, Clojure style
yoavrubin
 
PDF
What can scala puzzlers teach us
Daniel Sobral
 
PPTX
C++ overview
Prem Ranjan
 
PDF
Introduction to Python for Plone developers
Jim Roepcke
 
PDF
Real-World Scala Design Patterns
NLJUG
 
PPT
Scala Talk at FOSDEM 2009
Martin Odersky
 
PPTX
Practical type mining in Scala
Rose Toomey
 
Strong typing @ php leeds
Damien Seguy
 
Functional OOP, Clojure style
yoavrubin
 
What can scala puzzlers teach us
Daniel Sobral
 
C++ overview
Prem Ranjan
 
Introduction to Python for Plone developers
Jim Roepcke
 
Real-World Scala Design Patterns
NLJUG
 
Scala Talk at FOSDEM 2009
Martin Odersky
 
Practical type mining in Scala
Rose Toomey
 

What's hot (19)

PDF
Functional Programming In Practice
Michiel Borkent
 
PDF
Effective Scala (JavaDay Riga 2013)
mircodotta
 
PPTX
Introduction to Java Programming
One97 Communications Limited
 
PPT
Csharp_mahesh
Ananthu Mahesh
 
PDF
Introduction to Functional Programming with Scala
pramode_ce
 
PPTX
Let’s have some fun with Power Query M language
Cédric Charlier
 
PPTX
The Evolution of Scala
Martin Odersky
 
PDF
Introduction to Type Script by Sam Goldman, SmartLogic
SmartLogic
 
PDF
Ruby para-programadores-php
Juan Maiz
 
PDF
Ruby1_full
tutorialsruby
 
PPTX
Introduction to Scala
Rahul Jain
 
PPTX
Groovy Programming Language
Aniruddha Chakrabarti
 
PDF
Functional programming in kotlin with Arrow [Sunnytech 2018]
Emmanuel Nhan
 
ODP
A Tour Of Scala
fanf42
 
PDF
Scala reflection
David Pichsenmeister
 
PPT
String and string manipulation
Shahjahan Samoon
 
PDF
Few simple-type-tricks in scala
Ruslan Shevchenko
 
PPT
Php basics
hamfu
 
PDF
Type Profiler: Ambitious Type Inference for Ruby 3
mametter
 
Functional Programming In Practice
Michiel Borkent
 
Effective Scala (JavaDay Riga 2013)
mircodotta
 
Introduction to Java Programming
One97 Communications Limited
 
Csharp_mahesh
Ananthu Mahesh
 
Introduction to Functional Programming with Scala
pramode_ce
 
Let’s have some fun with Power Query M language
Cédric Charlier
 
The Evolution of Scala
Martin Odersky
 
Introduction to Type Script by Sam Goldman, SmartLogic
SmartLogic
 
Ruby para-programadores-php
Juan Maiz
 
Ruby1_full
tutorialsruby
 
Introduction to Scala
Rahul Jain
 
Groovy Programming Language
Aniruddha Chakrabarti
 
Functional programming in kotlin with Arrow [Sunnytech 2018]
Emmanuel Nhan
 
A Tour Of Scala
fanf42
 
Scala reflection
David Pichsenmeister
 
String and string manipulation
Shahjahan Samoon
 
Few simple-type-tricks in scala
Ruslan Shevchenko
 
Php basics
hamfu
 
Type Profiler: Ambitious Type Inference for Ruby 3
mametter
 
Ad

Similar to Strong typing : adoption, adaptation and organisation (20)

PDF
Nikita Popov "What’s new in PHP 8.0?"
Fwdays
 
PDF
What's new in PHP 8.0?
Nikita Popov
 
PPTX
PHP7 Presentation
David Sanchez
 
PDF
What's new in PHP 8.0?
Nikita Popov
 
PPTX
Php 5.4: New Language Features You Will Find Useful
David Engel
 
PDF
Typed Properties and more: What's coming in PHP 7.4?
Nikita Popov
 
PDF
Typed Drupal - A great combination of Drupal 8 and PHP 7
Aditya Ghan
 
PDF
What is new in PHP
Haim Michael
 
PDF
PHP 7.0 new features (and new interpreter)
Andrea Telatin
 
PDF
PHP 5.3 Overview
jsmith92
 
PDF
PHP to Hack at Slack
Scott Sandler
 
PPTX
PHP in 2018 - Q4 - AFUP Limoges
✅ William Pinaud
 
PDF
PHP 8: Process & Fixing Insanity
GeorgePeterBanyard
 
PPT
PHP 5.3 Part 1 - Introduction to PHP 5.3
melechi
 
PPTX
PHP 7 Crash Course - php[world] 2015
Colin O'Dell
 
PDF
TAKING PHP SERIOUSLY - Keith Adams
Hermes Alves
 
PDF
PHP7: Hello World!
Pavel Nikolov
 
PDF
PHP 8: What's New and Changed
Ayesh Karunaratne
 
PDF
Preparing for the next PHP version (5.6)
Damien Seguy
 
PDF
50 shades of PHP
Maksym Hopei
 
Nikita Popov "What’s new in PHP 8.0?"
Fwdays
 
What's new in PHP 8.0?
Nikita Popov
 
PHP7 Presentation
David Sanchez
 
What's new in PHP 8.0?
Nikita Popov
 
Php 5.4: New Language Features You Will Find Useful
David Engel
 
Typed Properties and more: What's coming in PHP 7.4?
Nikita Popov
 
Typed Drupal - A great combination of Drupal 8 and PHP 7
Aditya Ghan
 
What is new in PHP
Haim Michael
 
PHP 7.0 new features (and new interpreter)
Andrea Telatin
 
PHP 5.3 Overview
jsmith92
 
PHP to Hack at Slack
Scott Sandler
 
PHP in 2018 - Q4 - AFUP Limoges
✅ William Pinaud
 
PHP 8: Process & Fixing Insanity
GeorgePeterBanyard
 
PHP 5.3 Part 1 - Introduction to PHP 5.3
melechi
 
PHP 7 Crash Course - php[world] 2015
Colin O'Dell
 
TAKING PHP SERIOUSLY - Keith Adams
Hermes Alves
 
PHP7: Hello World!
Pavel Nikolov
 
PHP 8: What's New and Changed
Ayesh Karunaratne
 
Preparing for the next PHP version (5.6)
Damien Seguy
 
50 shades of PHP
Maksym Hopei
 
Ad

More from Damien Seguy (20)

PDF
Qui a laissé son mot de passe dans le code
Damien Seguy
 
PDF
Analyse statique et applications
Damien Seguy
 
PDF
Top 10 pieges php afup limoges
Damien Seguy
 
PDF
Top 10 php classic traps DPC 2020
Damien Seguy
 
PDF
Meilleur du typage fort (AFUP Day, 2020)
Damien Seguy
 
PDF
Top 10 php classic traps confoo
Damien Seguy
 
PDF
Tout pour se préparer à PHP 7.4
Damien Seguy
 
PDF
Top 10 php classic traps php serbia
Damien Seguy
 
PDF
Top 10 php classic traps
Damien Seguy
 
PDF
Top 10 chausse trappes
Damien Seguy
 
PDF
Code review workshop
Damien Seguy
 
PDF
Understanding static analysis php amsterdam 2018
Damien Seguy
 
PDF
Review unknown code with static analysis php ce 2018
Damien Seguy
 
PDF
Everything new with PHP 7.3
Damien Seguy
 
PDF
Php 7.3 et ses RFC (AFUP Toulouse)
Damien Seguy
 
PDF
Tout sur PHP 7.3 et ses RFC
Damien Seguy
 
PDF
Review unknown code with static analysis php ipc 2018
Damien Seguy
 
PDF
Code review for busy people
Damien Seguy
 
PDF
Static analysis saved my code tonight
Damien Seguy
 
PDF
Machine learning in php las vegas
Damien Seguy
 
Qui a laissé son mot de passe dans le code
Damien Seguy
 
Analyse statique et applications
Damien Seguy
 
Top 10 pieges php afup limoges
Damien Seguy
 
Top 10 php classic traps DPC 2020
Damien Seguy
 
Meilleur du typage fort (AFUP Day, 2020)
Damien Seguy
 
Top 10 php classic traps confoo
Damien Seguy
 
Tout pour se préparer à PHP 7.4
Damien Seguy
 
Top 10 php classic traps php serbia
Damien Seguy
 
Top 10 php classic traps
Damien Seguy
 
Top 10 chausse trappes
Damien Seguy
 
Code review workshop
Damien Seguy
 
Understanding static analysis php amsterdam 2018
Damien Seguy
 
Review unknown code with static analysis php ce 2018
Damien Seguy
 
Everything new with PHP 7.3
Damien Seguy
 
Php 7.3 et ses RFC (AFUP Toulouse)
Damien Seguy
 
Tout sur PHP 7.3 et ses RFC
Damien Seguy
 
Review unknown code with static analysis php ipc 2018
Damien Seguy
 
Code review for busy people
Damien Seguy
 
Static analysis saved my code tonight
Damien Seguy
 
Machine learning in php las vegas
Damien Seguy
 

Recently uploaded (20)

PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 

Strong typing : adoption, adaptation and organisation