87
UNKNOWN FEATURES OF PHP or less known ;)

Unknown features of PHP

Embed Size (px)

Citation preview

Page 1: Unknown features of PHP

UNKNOWN FEATURES OF PHP

or less known ;)

Page 2: Unknown features of PHP

ABOUT ME

CERTIFIEDPHP ENGINEER

CERTIFIEDARCH ITECT

2

Łukasz Witczak

10+ yrs experience

Page 3: Unknown features of PHP
Page 4: Unknown features of PHP

STRICT TYPED PHP?

Page 5: Unknown features of PHP

IS IT POSSIBLE TO WRITE STRICTLY TYPED PHP CODE?

Never trust accepted answer at stackoverflow

Page 6: Unknown features of PHP

STATIC/DYNAMIC STRICT/WEAK?

• Static/Dynamic typing is about when type information is aquired (at compiletime or at runtime)

• Strong/Weak typing is about how strictly types are distinguished (e.g. whether the language tries to do implicit conversion from strings to numbers).

Page 7: Unknown features of PHP

WEAK TYPING AND STRICT TYPING

• Fully strict type checking where no conversion happens (Go, Facebook's Hack)

• Widening primitive type checking where “safe” conversions happen (Java, Pascal, PHP Objects).

• Weak type checking which all conversions are allowed, with possible warnings raised (which is used to a limited extent by C, C#, C++ and Visual Basic, PHP scalars?)

Page 8: Unknown features of PHP

“DESPITE THEIR PRODUCTIVITY ADVANTAGES, DYNAMIC

LANGUAGES MAY NOT BE THE BEST CHOICES FOR ALL APPLICATIONS, PARTICULARLY FOR VERY LARGE

CODE BASES AND HIGH-PERFORMANCE APPLICATIONS.”

http://docs.zephir-lang.com/en/latest/motivation.html#statically-typed-versus-dynamically-typed-languages

Page 9: Unknown features of PHP

TYPE HINTING PHP 5+

Page 10: Unknown features of PHP

TYPE HINTING PHP7

Page 11: Unknown features of PHP

SCALAR TYPE HINTS FOR PHP 5.4+

• string

• integer, int

• float, double

• boolean, bool

• resource

https://pecl.php.net/package/strict

Page 12: Unknown features of PHP

LIMITATIONS

• “Internal functions cannot use strict hints, only user code is supported.”

• “Because the parser detects scalar hints as classes, they cannot have default values.” This was written before we knew that

PHP7 would get strict parameter and return typing.The best thing to do now is probably make this a close approximation of compatibility for PHP5 ... I'm super busy right now and this isn't really a priority …

Page 13: Unknown features of PHP

–Joe Watkins

“ I'm super busy right now and this isn't really a priority ...”

FUTURE

Page 14: Unknown features of PHP

SPL_TYPES

Page 15: Unknown features of PHP

http://php.net/manual/en/intro.spl-types.php

“This extension aims at helping people making PHP a stronger typed language and can be a good alternative

to scalar type hinting.”

SPL_TYPES

Page 16: Unknown features of PHP
Page 17: Unknown features of PHP
Page 18: Unknown features of PHP

demo?

Page 19: Unknown features of PHP
Page 20: Unknown features of PHP
Page 21: Unknown features of PHP
Page 22: Unknown features of PHP
Page 23: Unknown features of PHP

SPL_TYPES

• Total downloads: 53,633

Page 24: Unknown features of PHP

ZEPHIRZephir, an open source, high-level language designed to ease the

creation and maintainability of extensions for PHP with a focus on type and memory safety.

Page 25: Unknown features of PHP

ZEPHIR STATIC TYPES• boolean A boolean expresses a truth value. It can be either ‘true’ or ‘false’.

• integer Signed integers. At least 16 bits in size.

• unsigned integer Unsigned integers. At least 16 bits in size.

• char Smallest addressable unit of the machine that can contain basic character set.

• unsigned char Same size as char, but guaranteed to be unsigned.

• long Long signed integer type. At least 32 bits in size.

• unsigned long Same as long, but unsigned.

• float/double Double precision floating-point type. The size is platform-dependent.

• string A string is series of characters, where a character is the same as a byte.

• array An structure that can be used as hash, map, dictionary, collection, stack, etc.

Page 26: Unknown features of PHP

ZEPHIR UNSIGNED INT

Page 27: Unknown features of PHP

ZEPHIR CHAR AND STRING

Page 28: Unknown features of PHP

ASYNCHRONOUS PHPcooperative multitasking

Page 29: Unknown features of PHP
Page 30: Unknown features of PHP

SYNCHRONOUS

Tasks

Time

0 150 300 450 600

Task #1 Task #2 Task #3

Page 31: Unknown features of PHP

ASYNCHRONOUS

Tasks

Time [ms]

0 75 150 225 300

Task #1 Task #2 Task #3

Page 32: Unknown features of PHP

NODE.JS

Page 33: Unknown features of PHP
Page 34: Unknown features of PHP

EVENT-DRIVEN, NON-BLOCKING I/O WITH PHP

Page 35: Unknown features of PHP
Page 36: Unknown features of PHP

pond 3mln pobrań promise

Page 37: Unknown features of PHP

• StreamSelectLoop - out of the box

• LibEventLoop - This uses the libevent pecl extension with a number of system-specific backends (epoll, kqueue).

• LibEvLoop -This uses the libev pecl extension (github). It supports the same backends as libevent.

• ExtEventLoop - This uses the event pecl extension. It supports the same backends as libevent

loop

Page 38: Unknown features of PHP

by Marc J. Schmidt

Page 39: Unknown features of PHP

• http://marcjschmidt.de/blog/2014/02/08/php-high-performance.html

• https://github.com/php-pm/php-pmLibEvLoop

• http://reactphp.org/

• https://github.com/reactphp

resources

Page 40: Unknown features of PHP

https://github.com/ratchetphp/Ratchet

ratchetphp/Ratchet

Ratchet is a loosely coupled PHP library providing developers with tools to create real time, bi-directional applications between clients

and servers over WebSockets.

http://socketo.me/

Page 41: Unknown features of PHP

• Supports the RFC6455, HyBi-10+, and Hixie76 protocol versions (at the same time)

• Tested on Chrome 13+, Firefox 6+, Safari 5+, iOS 4.2+, IE 8+

• Ratchet passes the Autobahn Testsuite (non-binary messages)

Page 42: Unknown features of PHP

https://github.com/nrk/predis-async

Predis/Async

Page 43: Unknown features of PHP

https://github.com/bixuehujin/reactphp-mysql

react/mysql

Page 44: Unknown features of PHP

• mysql (with non blocking I/O) driver for reactphp,

• It is written in pure PHP - implemented the mysql protocol.

react/mysql

Page 45: Unknown features of PHP

\

Page 46: Unknown features of PHP

• Installs: 530

• Stars: 58

• Watchers: 11

• Forks: 13

• Open Issues: 3

react/mysql

Page 47: Unknown features of PHP

https://github.com/reactphp/filesystem

react/filesystem

Page 48: Unknown features of PHP

• Evented filesystem access utilising asyncronous POSIX I/O by means of libEIO C library

• requires http://php.net/eio extension

react/filesystem

Page 49: Unknown features of PHP

react/filesystem

Page 50: Unknown features of PHP

• Installs: 1 014

• Dependents: 2

• Stars: 33

• Watchers: 10

• Forks: 7

• Open Issues: 5

filesystem non-blocking I/O

Page 51: Unknown features of PHP

HACK LANG

• Async, Await, Awaitable

Page 52: Unknown features of PHP

HACK LANG

Page 53: Unknown features of PHP

HACK LANG

od hhvm 3.6 - obecnie hhvm 3.10

Page 54: Unknown features of PHP

ASYNC DB QUERIES

Page 55: Unknown features of PHP

ASYNC MYSQLmysqli::reap_async_querymysqli_reap_async_query

Page 56: Unknown features of PHP
Page 57: Unknown features of PHP

ASYNC POSTGRESQL

Page 58: Unknown features of PHP

ASYNC HTTP

Page 59: Unknown features of PHP

CURL_MULTI

Page 60: Unknown features of PHP
Page 61: Unknown features of PHP

BUZZ

Page 62: Unknown features of PHP

BUZZ\MULTI

Page 63: Unknown features of PHP

BUZZ VS BUZZ\MULTITi

me

[ms]

0

1250

2500

3750

5000

Sync Multi

Trudność w gromadzeniu requestów

Page 64: Unknown features of PHP

GUZZLE 5

END OF SCRIPT http://www.rfc-editor.org/rfc/rfc1945.txt 200 http://www.rfc-editor.org/rfc/rfc7540.txt 200 http://www.rfc-editor.org/rfc/rfc2068.txt 200

Page 65: Unknown features of PHP

composer require “guzzlehttp/guzzle:~5.3”

Page 66: Unknown features of PHP

THREADS

Page 67: Unknown features of PHP
Page 68: Unknown features of PHP

YES!PHP Threads…

Page 69: Unknown features of PHP

…and NOTpcntl_fork

Page 70: Unknown features of PHP
Page 71: Unknown features of PHP

https://pecl.php.net/package/pthreads

https://github.com/krakjoe/pthreads

http://php.net/pthreads

Page 72: Unknown features of PHP

PTHREADS

• Object Orientated (like Java)

• Easy to use an safety built in (no MUTEX)

• High level implementation of POSIX threads

• Well documented

Page 73: Unknown features of PHP

PTHREADS EXAMPLE

Page 74: Unknown features of PHP

PTHREADS

• Stable since 2013-09-08 (0.0.45)

• Over 163,000 downloads (from PECL)

• Heavily developed: (35 releases)

• Mature

Page 75: Unknown features of PHP
Page 76: Unknown features of PHP

PTHREADS

• http://pthreads.org/

• https://gist.github.com/krakjoe/9384409

• https://gist.github.com/krakjoe/6437782

read first

Page 77: Unknown features of PHP

PTHREADSNot for web

Page 78: Unknown features of PHP

–Joe Watkins

“(…)you can't use pthreads safely and sensibly anywhere but CLI.”

Page 79: Unknown features of PHP

PTHREADSPHP-CLI YES!

Page 80: Unknown features of PHP

THREDS ARE HARD

• https://web.stanford.edu/~ouster/cgi-bin/papers/threads.pdf

Page 81: Unknown features of PHP
Page 82: Unknown features of PHP

OPERATOR OVERLOADING

Page 83: Unknown features of PHP
Page 84: Unknown features of PHP
Page 85: Unknown features of PHP

OPERATOR OVERLOADING IN PHP EXTENSIONS

Page 86: Unknown features of PHP

OPERATOR OVERLOADING IN PHP EXTENSIONS

Page 87: Unknown features of PHP

THANK YOU

https://joind.in/16171

@woocashw