Upload
squidzce
View
320
Download
2
Embed Size (px)
Citation preview
UNKNOWN FEATURES OF PHP
or less known ;)
ABOUT ME
CERTIFIEDPHP ENGINEER
CERTIFIEDARCH ITECT
2
Łukasz Witczak
10+ yrs experience
STRICT TYPED PHP?
IS IT POSSIBLE TO WRITE STRICTLY TYPED PHP CODE?
Never trust accepted answer at stackoverflow
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).
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?)
“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
TYPE HINTING PHP 5+
TYPE HINTING PHP7
SCALAR TYPE HINTS FOR PHP 5.4+
• string
• integer, int
• float, double
• boolean, bool
• resource
https://pecl.php.net/package/strict
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 …
–Joe Watkins
“ I'm super busy right now and this isn't really a priority ...”
FUTURE
SPL_TYPES
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
demo?
SPL_TYPES
• Total downloads: 53,633
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.
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.
ZEPHIR UNSIGNED INT
ZEPHIR CHAR AND STRING
ASYNCHRONOUS PHPcooperative multitasking
SYNCHRONOUS
Tasks
Time
0 150 300 450 600
Task #1 Task #2 Task #3
ASYNCHRONOUS
Tasks
Time [ms]
0 75 150 225 300
Task #1 Task #2 Task #3
NODE.JS
EVENT-DRIVEN, NON-BLOCKING I/O WITH PHP
pond 3mln pobrań promise
• 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
by Marc J. Schmidt
• 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
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/
• 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)
https://github.com/bixuehujin/reactphp-mysql
react/mysql
• mysql (with non blocking I/O) driver for reactphp,
• It is written in pure PHP - implemented the mysql protocol.
react/mysql
\
• Installs: 530
• Stars: 58
• Watchers: 11
• Forks: 13
• Open Issues: 3
react/mysql
• Evented filesystem access utilising asyncronous POSIX I/O by means of libEIO C library
• requires http://php.net/eio extension
react/filesystem
react/filesystem
• Installs: 1 014
• Dependents: 2
• Stars: 33
• Watchers: 10
• Forks: 7
• Open Issues: 5
filesystem non-blocking I/O
HACK LANG
• Async, Await, Awaitable
HACK LANG
HACK LANG
od hhvm 3.6 - obecnie hhvm 3.10
ASYNC DB QUERIES
ASYNC MYSQLmysqli::reap_async_querymysqli_reap_async_query
ASYNC POSTGRESQL
ASYNC HTTP
CURL_MULTI
BUZZ
BUZZ\MULTI
BUZZ VS BUZZ\MULTITi
me
[ms]
0
1250
2500
3750
5000
Sync Multi
Trudność w gromadzeniu requestów
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
composer require “guzzlehttp/guzzle:~5.3”
THREADS
YES!PHP Threads…
…and NOTpcntl_fork
https://pecl.php.net/package/pthreads
https://github.com/krakjoe/pthreads
http://php.net/pthreads
PTHREADS
• Object Orientated (like Java)
• Easy to use an safety built in (no MUTEX)
• High level implementation of POSIX threads
• Well documented
PTHREADS EXAMPLE
PTHREADS
• Stable since 2013-09-08 (0.0.45)
• Over 163,000 downloads (from PECL)
• Heavily developed: (35 releases)
• Mature
PTHREADS
• http://pthreads.org/
• https://gist.github.com/krakjoe/9384409
• https://gist.github.com/krakjoe/6437782
read first
PTHREADSNot for web
–Joe Watkins
“(…)you can't use pthreads safely and sensibly anywhere but CLI.”
PTHREADSPHP-CLI YES!
THREDS ARE HARD
• https://web.stanford.edu/~ouster/cgi-bin/papers/threads.pdf
OPERATOR OVERLOADING
OPERATOR OVERLOADING IN PHP EXTENSIONS
OPERATOR OVERLOADING IN PHP EXTENSIONS