What's New In PHP7

41
What’s New In PHP7 Petra Novandi Barus Chief Technology Officer UrbanIndo.com Contact Me! email: [email protected] twitter: @petrabarus telegram: petrabarus github: petrabarus Deep insight Laravel, Magento, dan PHP7 20th February 2016 D.Lab, Jakarta

Transcript of What's New In PHP7

Page 1: What's New In PHP7

What’s New In PHP7Petra Novandi Barus

Chief Technology OfficerUrbanIndo.com

Contact Me!email: [email protected] twitter: @petrabarustelegram: petrabarusgithub: petrabarus

Deep insight Laravel, Magento, dan PHP720th February 2016

D.Lab, Jakarta

Page 2: What's New In PHP7

Who Am I?

Page 3: What's New In PHP7

- Chief Technology Officer, UrbanIndo.com- Master of Informatics Engineering ITB- Have been using PHP since 2003- Writes Yii2 libraries - Interests: Distributed systems, High

performance computing, Programming- Hobby: Sleeping, reading books

Page 4: What's New In PHP7

I really love reading about technologies and share the articles on my Twitter!

Follow my reading list at @petrabarus Let’s learn stuffs together!

Page 5: What's New In PHP7

What Am I Going To Talk About?

Page 6: What's New In PHP7

What’s New In PHP7?

What’s New In PHP7 (Internally)?What makes PHP7 much faster and

what can I learn from that?

How do the new PHP7 helps me to code?

Page 7: What's New In PHP7

So, what’s new in PHP7?

Page 8: What's New In PHP7

1. Scalar Type Hints

Page 9: What's New In PHP7

1. Scalar Type Hints Reasons

Type safety Detect bugs before run time Static analysis More robust code

Weak versus Strong Weak: coercive, will cast on different type Strong: strict, will throw TypeError.

Page 10: What's New In PHP7

2. Return Type Declaration

Similar reason with Type Hinting

Page 11: What's New In PHP7

3. Combined Comparison Operator (spaceship)

PHP 5PHP 7

Page 12: What's New In PHP7

3. Combined Comparison Operator (spaceship)

PHP 5

PHP 7

Page 13: What's New In PHP7

4. Null Coalesce Operator (??)

Page 14: What's New In PHP7

5. Anonymous Class Creates an object of a class without defining the class. Pass the value of outer class / object using the

constructor.

Page 15: What's New In PHP7

5. Anonymous Class

Page 16: What's New In PHP7

5. Anonymous Class

PHP 5

PHP 7

Page 17: What's New In PHP7

5. Anonymous Class

Page 18: What's New In PHP7

5. Anonymous Class Usage

Creating a very simple class, one time use. Quickly override one or few methods Implements a specific usage of an interface without defining new class (logger, event observer).

Mocking a class on the fly for testing purposes.

Page 19: What's New In PHP7

6. PerformanceAccording to HHVM…..

Page 20: What's New In PHP7

6. PerformanceAccording to Zend…..

Page 22: What's New In PHP7

CPU Cache & Memory Architecture

Size LatencyL1 Cache 32kB 1nsL2 Cache 256kB 4nsL3 Cache 4000kB 12nsMemory 2000+MB 100ns

A bit intro to low level optimization by utilizing CPU cache and memory architecture

Page 23: What's New In PHP7

CPU Cache & Memory Architecture

Much faster because using CPU cache

Consider these C codes

Page 24: What's New In PHP7

CPU Cache & Memory Architecture

Reference:https://software.intel.com/en-us/articles/how-to-use-loop-blocking-to-optimize-memory-use-on-32-bit-intel-architecture

Page 25: What's New In PHP7

Memory Optimization Reduce the number of allocations

PHP5 spends 20% of CPU time in allocator

Memory

Page 26: What's New In PHP7

Memory Optimization Reduce Memory Usage

Memory access has high latency Less data => more fits to cache

Page 27: What's New In PHP7

Memory Optimization Reduce Indirection

Remove the number of pointers use

Page 28: What's New In PHP7

Zval Zval is how PHP stores the variables (boolean, integer,

string, array). Zval is short of Zend Value. In PHP 7, Zval is optimized.

Less allocation Less indirection Less memory usage

Page 29: What's New In PHP7

Zval

PHP 5 PHP 7

Page 30: What's New In PHP7

ZvalPHP5- Zvals always

require heap allocation

- Zvals always reference counted and have cycle collection

PHP7- Zvals are no longer

heap-allocated and no ref count.

- Ref count in the complex value

- No double refcount. - Complex type can be

shared

Page 31: What's New In PHP7

ZVal PHP5

Page 32: What's New In PHP7

ZValPHP7

Page 33: What's New In PHP7

More Data Structure Optimization HashTable (array)

4 pointer indirections & 100~ byte memory to 2 pointer indirections & 32 byte memory.

String management Refcounted

Page 34: What's New In PHP7

AST PHP 7 introduces Abstract Syntax Tree Instead of single pass parse-compile, PHP7 will do

syntax analysis and then parse

Page 35: What's New In PHP7

ASTPHP5

PHP7

Page 36: What's New In PHP7

AST

PHP5

Page 37: What's New In PHP7

AST

PHP7

Page 38: What's New In PHP7

2. AST Well designed and well maintain Better syntax checking and consistency Slower to run but produce much better runtime

opcode Use opcode cache for this

Breaks backward compatibility

Page 40: What's New In PHP7

Conclusion PHP7 is way better than PHP5

Much faster Use less memory Produce robust code (Check your backward compatibility due to AST change)

You should use PHP7!

Page 41: What's New In PHP7

Questions?