SlideShare a Scribd company logo
1 of 63
Download to read offline
Typed Properties and more:
What’s coming in PHP 7.4?
Nikita Popov
Release schedule (tentative)
Now
June 6th PHP 7.4 Alpha 1
July 18th PHP 7.4 Beta 1 – Feature freeze
November 21st PHP 7.4 GA Release
Release schedule (tentative)
Now
June 6th PHP 7.4 Alpha 1
July 18th PHP 7.4 Beta 1 – Feature freeze
November 21st PHP 7.4 GA Release
December 2020 PHP 8.0 Release
≈
Overview
●
FFI & Preloading
●
Typed Properties
●
Arrow Functions
●
Covariant Return Types
●
??=
●
Array Spread Operator
●
WeakReference
●
Deprecations
Overview
●
FFI & Preloading => See Dmitry Stogov’s talk!
●
Typed Properties
●
Arrow Functions
●
Covariant Return Types
●
??=
●
Array Spread Operator
●
WeakReference
●
Deprecations
Typed Properties
class User {
public int $id;
public string $name;
}
$user = new User;
$user->id = 42;
$user->name = [];
// Uncaught TypeError: Typed property User::$name
// must be string, array used
Nullability & Initialization
class User {
public int $id; // no null default
public ?string $name; // also no null default (!)
}
Nullability & Initialization
class User {
public int $id; // no null default
public ?string $name; // also no null default (!)
}
$user = new User;
var_dump($user->name);
// Uncaught Error: Typed property User::$name must
// not be accessed before initialization
Nullability & Initialization
class User {
public int $id;
public ?string $name = null;
}
$user = new User;
var_dump($user->name); // NULL
References
class User {
public int $id = 42;
public string $name = "nikic";
}
$user = new User;
$id =& $user->id;
$id = "not an id";
// Uncaught TypeError: Cannot assign string to
// reference held by property User::$id of type int
Poor Man’s Intersection Types
class Test {
public ?Traversable $traversable;
public ?Countable $countable;
public $both = null;
}
$test = new Test;
$test->traversable =& $test->both;
$test->countable =& $test->both;
$test->both = new ArrayIterator;
Poor Man’s Intersection Types
class Test {
public ?Traversable $traversable;
public ?Countable $countable;
public $both = null;
}
$test = new Test;
$test->traversable =& $test->both;
$test->countable =& $test->both;
$test->both = new ArrayIterator;
Effectively ?(Traversable&Countable)
Poor Man’s Intersection Types
class Test {
public ?Traversable $traversable;
public ?Countable $countable;
public $both = null;
}
$test = new Test;
$test->traversable =& $test->both;
$test->countable =& $test->both;
$test->both = new AppendIterator;
// Uncaught TypeError: Cannot assign AppendIterator
// to reference held by property Test::$countable
// of type ?Countable
Poor Man’s Typed Variables
int $i = 0;
$i = "foobar";
Poor Man’s Typed Variables
$i =& int(0);
$i = "foobar";
// Uncaught TypeError: Cannot assign string to
// reference held by property class@anonymous::$prop
// of type int
Poor Man’s Typed Variables
function &int(int $i) {
$obj = new class { public int $prop; };
$obj->prop = $i;
$GLOBALS['huge_memory_leak'][] = $obj;
return $obj->prop;
}
$i =& int(0);
$i = "foobar";
// Uncaught TypeError: Cannot assign string to
// reference held by property class@anonymous::$prop
// of type int
Public Typed Properties?
class User {
private $name;
public function getName(): string {
return $this->name;
}
public function setName(string $name): void {
$this->name = $name;
}
}
Public Typed Properties?
class User {
public string $name;
}
Public Typed Properties?
class User {
public string $name;
}
What if additional validation will be needed?
Future: Property Accessors?
class User {
public string $name {
get;
set($name) {
if (strlen($name) == 0)
throw new Exception(
'Name cannot be empty');
$this->name = $name;
}
};
}
Arrow Functions
array_filter(
$values,
function($v) use($allowedValues) {
return in_array($v, $allowedValues);
}
);
Arrow Functions
array_filter(
$values,
fn($v) => in_array($v, $allowedValues)
);
Arrow Functions
array_filter(
$values,
fn($v) => in_array($v, $allowedValues)
);
Implicit use($allowedValues)
By-Value Binding
$i = 0;
$fn = fn() => $i++;
$fn();
var_dump($i); // int(0)
By-Value Binding
$fns = [];
foreach ([1, 2, 3] as $i) {
$fns[] = fn() => $i;
}
foreach ($fns as $fn) {
echo $fn() . " ";
}
// Prints: 1 2 3
// Not: 3 3 3 (would happen with by-reference)
Syntax – Why fn?
array_filter(
$values,
$v => in_array($v, $allowedValues)
);
ECMAScript syntax
Syntax – Array Ambiguity
$array = [
$a => $a + $b,
$x => $x * $y,
];
Array of arrow functions?
Or just a key-value map?
Syntax – Parsing
// Looks like: Assignment expression
($x = [42] + ["foobar"]) => $x;
// Looks like: Constant lookup + bitwise and
(Type &$x) => $x;
// Looks like: Ternary operator
$a ? ($b): Type => $c : $d;
Syntax – Parsing
// Looks like: Assignment expression
($x = [42] + ["foobar"]) => $x;
// Looks like: Constant lookup + bitwise and
(Type &$x) => $x;
// Looks like: Ternary operator
$a ? ($b): Type => $c : $d;
Need special handling starting at (
But only know it’s an arrow function at =>
Future: Block Syntax
array_filter(
$values,
fn($v) => in_array($v, $allowedValues)
);
Future: Block Syntax
array_filter(
$values,
fn($v) {
// More code...
return in_array($v, $allowedValues);
}
);
Future: Block Syntax
array_filter(
$values,
fn($v) {
// More code...
return in_array($v, $allowedValues);
}
);
Basically like normal closure syntax,
but with implicit variable capture
Covariant Return Types
class A {}
class B extends A {}
class Producer {
public function produce(): A {}
}
class ChildProducer extends Producer {
public function produce(): B {}
}
Sound because $childFactory->create() still instanceof A.
Common Case: Self
class Foo {
public function fluent(): self {}
}
class Bar extends Foo {
public function fluent(): self {}
}
Ordering Issues
class A {
public function method(): B {}
}
class B extends A {
public function method(): C {}
}
class C extends B {}
Ordering Issues
class A {
public function method(): B {}
}
class B extends A {
public function method(): C {}
}
class C extends B {}
Sound, but class C not available when verifying B.
=> No way to reorder classes “correctly”.
=> Will only work with autoloading (for now).
Contravariant Parameter Types
class A {}
class B extends A {}
class Consumer {
public function comsume(B $value) {}
}
class ChildConsumer extends Consumer {
public function comsume(A $value) {}
}
Sound, but rarely useful.
Covariant Parameter Types?
interface Event { ... }
class SpecificEvent implements Event { ... }
interface EventHandler {
public function handle(Event $e);
}
class SpecificEventHandler implements EventHandler {
public function handle(SpecificEvent $e) {}
}
Unsound, will never work!
Future: Generics
interface Event { ... }
class SpecificEvent implements Event { ... }
interface EventHandler<E: Event> {
public function handle(E $e);
}
class SpecificEventHandler
implements EventHandler<SpecificEvent>
{
public function handle(SpecificEvent $e) {}
}
??=
$options["something"] ??= new DefaultObject;
// Roughly equivalent to:
$options["something"] =
$options["something"] ?? new DefaultObject;
??=
$options["something"] ??= new DefaultObject;
Only created if $options["something"] is null
Array spread operator
$array = [3, 4, 5];
return call(1, 2, ...$array); // call(1, 2, 3, 4, 5)
$array = [3, 4, 5];
return [1, 2, ...$array]; // [1, 2, 3, 4, 5]
Array spread operator
$array = new ArrayIterator([1, 2, 3]);
return call(1, 2, ...$array); // call(1, 2, 3, 4, 5)
$array = new ArrayIterator([1, 2, 3]);
return [1, 2, ...$array]; // [1, 2, 3, 4, 5]
Array spread operator
$array = ['y' => 'b', 'z' => 'c'];
return ['x' => 'a', ...$array];
// Uncaught Error: Cannot unpack array with
// string keys
Future: … in destructuring
[$head, ...$tail] = $array;
Future: … in destructuring
$array = [2 => 2, 1 => 1, 0 => 0];
[$head, ...$tail] = $array;
What happens?
WeakReference
$object = new stdClass;
$weakRef = WeakReference::create($object);
var_dump($weakRef->get()); // object(stdClass)#1
unset($object);
var_dump($weakRef->get()); // NULL
WeakReference
$this->data[get_object_id($object)]
= [WeakReference::create($object), $data];
WeakReference
$this->data[get_object_id($object)]
= [WeakReference::create($object), $data];
Will be reused once
object is destroyed
WeakReference
$this->data[get_object_id($object)]
= [WeakReference::create($object), $data];
Will be reused once
object is destroyed
Check $weakRef->get() != null
to know whether the entry is still valid
WeakReference
$this->data[get_object_id($object)]
= [WeakReference::create($object), $data];
Will be reused once
object is destroyed
Check $weakRef->get() != null
to know whether the entry is still valid
Problem: Doesn’t keep $object alive, but keeps dead cache entry
Future: WeakMap
$this->data = new WeakMap();
$this->data[$object] = $data;
Does not keep $object alive; immediately
drops cache entry when object destroyed
Deprecations
Surprisingly few for now…
…there will probably be more.
Ternary Associativity
return $a == 1 ? 'one'
: $a == 2 ? 'two'
: 'other';
// was intended as:
return $a == 1 ? 'one'
: ($a == 2 ? 'two'
: 'other');
// but PHP interprets it as:
return ($a == 1 ? 'one'
: $a == 2) ? 'two'
: 'other';
Ternary Associativity
return $a == 1 ? 'one' // Deprecated in 7.4.
: $a == 2 ? 'two' // Compile error in 8.0.
: 'other';
// was intended as:
return $a == 1 ? 'one'
: ($a == 2 ? 'two'
: 'other');
// but PHP interprets it as:
return ($a == 1 ? 'one'
: $a == 2) ? 'two'
: 'other';
Concatenation Precedence
$a = 1;
$b = 2;
echo "Sum: " . $a+$b;
// was intended as:
echo "Sum: " . ($a+$b);
// but PHP interprets it as:
echo ("Sum: " . $a)+$b;
Concatenation Precedence
$a = 1;
$b = 2;
echo "Sum: " . $a+$b; // Deprecated in PHP 7.4
// was intended as:
echo "Sum: " . ($a+$b); // New behavior in PHP 8.0
// but PHP interprets it as:
echo ("Sum: " . $a)+$b;
Short Open Tags (?)
●
<? deprecated in 7.4, removed in 8.0
●
Only <?php and <?= supported
●
(???) short_open_tag default value from On to
Off in 7.4
Disclaimer: RFC accepted, but much push-back after voting.
3v4l.org
Travis CI
php:
- 7.3
- 7.4snapshot
- nightly
install:
- composer install --ignore-platform-reqs
PHPUnit claims it is not
PHP 8 compatible (it is)PHP 8
Docker
●
https://github.com/devilbox/docker-php-fpm-7.4
●
https://github.com/devilbox/docker-php-fpm-8.0
Thank You!
Questions?

More Related Content

Similar to Typed Properties and more: What's coming in PHP 7.4?

JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
jsmith92
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 

Similar to Typed Properties and more: What's coming in PHP 7.4? (20)

JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
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
 
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
 
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
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
 
Hack tutorial
Hack tutorialHack tutorial
Hack tutorial
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
Smelling your code
Smelling your codeSmelling your code
Smelling your code
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
Mocking Demystified
Mocking DemystifiedMocking Demystified
Mocking Demystified
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developers
 
PHP Tips & Tricks
PHP Tips & TricksPHP Tips & Tricks
PHP Tips & Tricks
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 

More from Nikita Popov

More from Nikita Popov (10)

A whirlwind tour of the LLVM optimizer
A whirlwind tour of the LLVM optimizerA whirlwind tour of the LLVM optimizer
A whirlwind tour of the LLVM optimizer
 
Opaque Pointers Are Coming
Opaque Pointers Are ComingOpaque Pointers Are Coming
Opaque Pointers Are Coming
 
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?
 
Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8
 
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 Performance Trivia
PHP Performance TriviaPHP Performance Trivia
PHP Performance Trivia
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
 
PHP 7 – What changed internally?
PHP 7 – What changed internally?PHP 7 – What changed internally?
PHP 7 – What changed internally?
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Typed Properties and more: What's coming in PHP 7.4?