Github:Â https://github.com/matiux
Slack GRUSP:Â matiux
Email:Â m.galacci@gmail.com
Linkedin: Matteo Galacci
Enrico Zimuel
Â
Â
Â
Native unicode support to PHP.
Never released. Project abandoned.
Â
PHP 6 was supposed to add native unicode support to PHP. The community of developers working on it abandoned the project.
The main reason for not having a PHP 6 version is marketing. As time went on, it gave the PHP 6 release a bad name: a version that went in development for years (since 2005), but was never released.
Â
Unicode is a computing industry standard for the consistent encoding, representation, and handling of text expressed in most of the world's writing systems.
*Disabled by default. Use declare(strict_types=1); in each file
More info here
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
//int(9)
<?php
// strict mode
declare(strict_types=1);
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Fatal errors: Uncaught TypeError: Argument 2 passed to sumOfInts() must be of the type integer, string given
Â
Next TypeError: Argument 3 passed to sumOfInts() must be of the type integer, float given, called in
<?php
function arraysSum(array ...$arrays): array
{
return array_map(function(array $array): int {
return array_sum($array);
}, $arrays);
}
print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
Array
(
[0] => 6
[1] => 15
[2] => 24
)
<?php
interface Logger {
public function log(string $msg);
}
class Application {
private $logger;
public function getLogger(): Logger {
return $this->logger;
}
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application();
$app->setLogger(new class implements Logger {
public function log(string $msg) {
echo $msg;
}
});
var_dump($app->getLogger());
<?php
function testReturn(?string $string): ?string
{
return $string;
}
echo testReturn(null); // null
echo testReturn('foo'); // foo
<?php
function testReturn(string $string): void
{
echo $string;
}
echo testReturn('foo'); // foo
<?php
class ConstDemo
{
const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
<?php
function test(object $obj) : object
{
return new SplQueue();
}
test(new StdClass());
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
<?php
try {
// some code
} catch (FirstException | SecondException $e) {
// handle first and second exceptions
}
PHPNG, PHP Next Generation
Developed by Dmitry Stogov and his team after more than a year's work.
He tried to make a JIT compiler like compiled or pseudo compiled languages (like Facebook with HHVM).
Better than PHP5, but not so much.
New data structure management (How variables, arrays, hash tables are managed. Everything, the basics) in the PHP engine.
Great performance improvement.
Mandelbrot fractal generator. Math functions with many iterations.
PHP 7 is also faster than Python 3.
May 2017
May 2017
Requests per second
May 2017
May 2017
Resource Usage (CPU time):
May 2017
Memory Usage:
$a = [];
for ($i = 0; $i < 1000000; $i++) {
$a[$i] = ["hello"];
}
echo memory_get_usage(true);
PHP 5.6 | PHP 7 | |
---|---|---|
Memory usage | 428 MB | 33 MB |
Execution time | 0.49 sec | 0.06 sec |
Some benchmarking of PHP 7.4 compared with the previous versions.
Â
It has been used the Zend/bench.php script to run the experiment, getting the total execution times. This script is included with the PHP source code and it was created for testing the language using some math operators, nested loops, array, strings and recursive functions.
Here the results (in seconds) for each PHP versions:
Here the results (in seconds) for each PHP versions:
Here the results (in seconds) for each PHP versions:
PHP 7.0 Nginx vs PHP 7.0 Swoole vs Node.js
http://grigorov.website/blog/performance-comparison-php-vs-node-js
Compared with other async programming frameworks or softwares such as Nginx, Tornado, Node.js, Swoole has the built-in PHP coroutine and async support, multiple threads I/O modules.
November 2019
array_map(function (User $user) {
return $user->id;
}, $users)
Also called "short closures".
array_map(fn (User $user) => $user->id, $users)
class ParentType {}
class ChildType extends ParentType {}
class A
{
public function covariantReturnTypes(): ParentType
{
/* … */
}
}
class B extends A
{
public function covariantReturnTypes(): ChildType
{
/* … */
}
}
Covariance
class ParentType {}
class ChildType extends ParentType {}
class A
{
public function contraVariantArguments(ChildType $type)
{
/* … */
}
}
class B extends A
{
public function contraVariantArguments(ParentType $type)
{
/* … */
}
}
Contravariance
A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another.
Â
Currently, accessing FFI data structures is significantly (about 2 times) slower than accessing native PHP arrays and objects. Therefore, it makes no sense to use the FFI extension for speed; however, it may make sense to use it to reduce memory consumption.
It was already possible to extend the PHP core. Phalcon framework is an example
Another lower-level feature is preloading. It's is an amazing addition to PHP's core, which can result in some significant performance improvements.
In short: if you're using a framework, its files have to be loaded and linked on every request. Preloading allows the server to load PHP files in memory on startup, and have them permanently available to all subsequent requests.
The performance gain comes of course with a cost: if the source of preloaded files are changed, the server has to be restarted.
Weak references allow the programmer to retain a reference to an object which does not prevent the object from being destroyed. They are useful for implementing cache like structures.
$obj = new stdClass;
$weakref = WeakReference::create($obj);
var_dump($weakref->get());
unset($obj);
var_dump($weakref->get());
/*
object(stdClass)#1 (0) {
}
NULL
*/
$a = new stdClass;
$b =&$a;
var_dump($a);
var_dump($b);
unset($a);
var_dump($a);
var_dump($b);
/*
object(stdClass)#1 (0) {
}
object(stdClass)#1 (0) {
}
Notice: Undefined variable: a...
NULL
object(stdClass)#1 (0) {
}
*/
$data['date'] = $data['date'] ?? new DateTime();
$data['date'] ??= new DateTime();
$arrayA = [1, 2, 3];
$arrayB = [4, 5];
$result = [0, ...$arrayA, ...$arrayB, 6 ,7];
// [0, 1, 2, 3, 4, 5, 6, 7]
Only works with arrays with numerical keys.
PHP 8, the new major PHP version, is expected to be released on December 3, 2020. That means there will be no PHP 7.5 version.
A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into machine code, and then invoke this object code instead
Ciclo di vita
<?php
for ($i=0; $i<100; $i++) {
echo $i;
}
Example
Opcode
L0 (2): ASSIGN CV0($i) int(0)
L1 (2): JMP L4
L2 (3): ECHO CV0($i)
L3 (2): PRE_INC CV0($i)
L4 (2): T1 = IS_SMALLER CV0($i) int(100)
L5 (2): JMPNZ T1 L2
L6 (5): RETURN int(1)
Linguaggio semplice simile ad assembly
Opcache
JIT
JIT
Previously we said that JIT can decide which portions to translate into machine code and which to leave in Opcode. How do you decide? Through a CRTO configuration, a number of 4 decimal places where each number represents a specific configuration.
CRTO = 1235
Â
JIT TRIGGER
* Useful for developing libraries and frameworks
<?php
class Number {
private int|float $number;
public function setNumber(int|float $number): void {
$this->number = $number;
}
public function getNumber(): int|float {
return $this->number;
}
}
<?php
class A {
public function __construct($params = []) {
$this->params = $params;
}
public function create($params): static {
return new static($params);
}
}
class B extends A {
}
$b = new B();
$result = $b->create(['x' => 'y']);
var_dump($result); // object(B) {["params"]=> ["x" => "y"]}
Useful in fluent interfaces
<?php
$map = new WeakMap;
$obj = new stdClass;
$map[$obj] = 42;
var_dump($map);
// object(WeakMap)#1 (1) {
// [0]=> [
// "key" => object(stdClass)#2,
// "value" => int(42)
// ]
// }
// The object is destroyed here, key automatically removed
unset($obj);
var_dump($map);
// object(WeakMap)#1 (0) { }
To provide "weakly referenced" map objects
Useful in the implementation of cache systems
<?php
class Foo {
private WeakMap $cache;
public function getSomethingWithCaching(object $obj) {
return $this->cache[$obj] ??= $this->somethingExpensive($obj);
}
}
To provide "weakly referenced" map objects
??= Null Coalescing Assignment (PHP 7.4+)
When the $obj variable is cleared somewhere, I don't have to invalidate the cache because the WeakMaps clears the reference in the cache
<?php
interface Stringable
{
public function __toString(): string;
}
Stringable interface is automatically added to classes that implement the __toString() method
Feature freeze: July 28th 2020
Release date: December 2020
PHP RFC official page
PHP RFC Watch
Brent Roose, New in PHP 8
Eli White, What’s in PHP Eight?, php[architect], Vol.19 - Issue 5, May 2020
Arkadiusz Kondas, Compiling PHP 8 from source with JIT support
NÃckolas Da Silva, Understanding PHP 8's JIT
Benoit Jacquemont, PHP 8 et Just In Time Compilation, PHPForum 2019
Nikita Popov, PHP 7 Virtual Machine
Anthony Ferrara, A PHP Compiler, aka The FFI Rabbit Hole
James Titcumb, Climbing the Abstract Syntax Tree, PHP UK 2018