Rasmus Lerdorf
@rasmus
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && \
(x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))
int htoi(char *s) {
int value;
char c;
c = s[0];
if(isupper(c)) c = tolower(c);
value=(c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
c = s[1];
if(isupper(c)) c = tolower(c);
value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
return(value);
}
void main(int argc, char *argv[]) {
char *params, *data, *dest, *s, *tmp;
char *name, *age;
puts("Content-type: text/html\r\n");
puts("<HTML><HEAD><TITLE>Form Example</TITLE></HEAD>");
puts("<BODY><H1>My Example Form</H1>");
puts("<FORM action=\"form.cgi\" method=\"GET\">");
puts("Name: <INPUT type=\"text\" name=\"name\">");
puts("Age: <INPUT type=\"text\" name=\"age\">");
puts("<BR><INPUT type=\"submit\">");
puts("</FORM>");
data = getenv("QUERY_STRING");
if(data && *data) {
params = data; dest = data;
while(*data) {
if(*data=='+') *dest=' ';
else if(*data == '%' && ishex(*(data+1))&&ishex(*(data+2))) {
*dest = (char) htoi(data + 1);
data+=2;
} else *dest = *data;
data++;
dest++;
}
*dest = '\0';
s = strtok(params,"&");
do {
tmp = strchr(s,'=');
if(tmp) {
*tmp = '\0';
if(!strcmp(s,"name")) name = tmp+1;
else if(!strcmp(s,"age")) age = tmp+1;
}
} while(s=strtok(NULL,"&"));
printf("Hi %s, you are %s years old\n",name,age);
}
puts("</BODY></HTML>");
}
use CGI qw(:standard);
print header;
print start_html('Form Example'),
h1('My Example Form'),
start_form,
"Name: ", textfield('name'),
p,
"Age: ", textfield('age'),
p,
submit,
end_form;
if(param()) {
print "Hi ",em(param('name')),
"You are ",em(param('age')),
" years old";
}
print end_html;
<html><head><title>Form Example</title></head>
<body><h1>My Example Form</h1>
<form action="form.phtml" method="POST">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
<br><input type="submit">
</form>
<?if($name):?>
Hi <?echo $name?>, you are <?echo $age?> years old
<?endif?>
</body></html>
✔ engine improvements
Improve CPU cache usage
$a = [];
for($i=0; $i < 100000;$i++) {
$a[] = ['abc','def','ghi','jkl','mno','pqr'];
}
echo memory_get_usage(true);
// PHP 5.x 109M
// PHP 7.0 42M no opcache
// PHP 7.0 6M with opcache enabled
GCC Feedback-Directed Optimization (FDO)
$ gcc --version
gcc (Debian 6.3.0-6) 6.3.0 20170205
$ make clean
$ make -j8 prof-gen
...
$ sapi/cgi/php-cgi -T 1000 /var/www/wordpress/index.php > /dev/null
$ make prof-clean
$ make -j8 prof-use
At 5% Adoption
At 100% Adoption
engine improvements
Exceptions on Fatals
function call_method($obj) {
$obj->method();
}
call_method(null);
Fatal error: Uncaught Error: Call to a member function method() on null in file:2
Stack trace:
#0 file(4): call_method(NULL)
#1 {main}
thrown in file on line 2
try {
call_method(null);
} catch (Error $e) {
echo "Caught Exception: {$e->getMessage()}\n";
}
Caught Exception: Call to a member function method() on null
PHP 7 Exception Hierarchy
Return Types
function get_config(): array {
return 42;
}
get_config();
Fatal error: Uncaught TypeError: Return value of get_config() must be
of the type array, integer returned in file:2
Stack trace:
#0 file(4): get_config()
#1 {main}
thrown in file on line 2
Coercive Scalar Types
function logmsg(string $msg, int $level, float $severity) {
var_dump($msg); // string(1) "1"
var_dump($level); // int(2)
var_dump($severity); // float(3)
}
logmsg(1, "2.5 bananas", 3);
Notice: A non well formed numeric value encountered in file on line 2
Strict Scalar Types
declare(strict_types=1);
...
logmsg(1, "2.5", 3);
Fatal error: Uncaught TypeError: Argument 1 passed to logmsg() must be of the
type string, integer given, called in file on line 7 and defined in file:3
Stack trace:
#0 file(7): logmsg(1, '2.5', 3)
#1 {main}
thrown in file on line 2
Anonymous Classes
return new class($controller) implements Page {
public function __construct($controller) {
/* ... */
}
/* ... */
};
class MyObject extends MyStuff {
public function getInterface() {
return new class implements MyInterface {
/* ... */
};
}
}
Coalesce Operator
$a = NULL;
$b = 0;
$c = 2;
echo $a ?? $b; // 0
echo $c ?? $b; // 2
echo $a ?? $b ?? $c; // 0
echo $a ?? $x ?? $c; // 2
Spaceship Operator
function cmp_php5($a, $b) {
return ($a < $b) ? -1 : (($a >$b) ? 1 : 0);
}
function cmp_php7($a, $b) {
return $a <=> $b;
}
Removal of many deprecated features
(Your PHP4 code will break!)
- ext/ereg (use ext/pcre instead)
- preg_replace() eval modifier (use preg_replace_callback() instead)
- ext/mysql (use ext/mysqli or ext/pdo_mysql instead)
- Assignment of new by reference
- Scoped calls of non-static methods from incompatible $this context
- dl() in php-fpm
- set_magic_quotes_runtime() and magic_quotes_runtime()
- set_socket_blocking() (use stream_set_blocking() instead)
- mcrypt_generic_end() (use mcrypt_generic_deinit() instead)
- mcrypt_ecb, mcrypt_cbc, mcrypt_cfb and mcrypt_ofb
(use mcrypt_encrypt() and mcrypt_decrypt() instead)
- datefmt_set_timezone_id() and IntlDateFormatter::setTimeZoneID()
(use datefmt_set_timezone() or IntlDateFormatter::setTimeZone() instead)
- xsl.security_prefs (use XsltProcessor::setSecurityPrefs() instead)
- iconv.input_encoding, iconv.output_encoding, iconv.internal_encoding,
mbstring.http_input, mbstring.http_output and mbstring.internal_encoding
(use php.input_encoding, php.internal_encoding and php.output_encoding instead)
- $is_dst parameter of the mktime() and gmmktime() functions
- # style comments in ini files (use ; style comments instead)
- String category names in setlocale() (use LC_* constants instead)
- Unsafe curl file uploads (use CurlFile instead)
- PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT driver option
(use PDO::ATTR_EMULATE_PREPARES instead)
- CN_match and SNI_server_name stream context option (use peer_name instead)
New reserved words:
64-bit integer support on Windows
Cleanup edge-case integer overflow/underflow
Support for strings with length >= 2^31 bytes in 64 bit builds.
Parse error on invalid numeric literals
$mask = 0855; // Parse error: Invalid numeric literal
Uniform variable syntax
// left-to-right
$this->$belongs_to['column']
// vs.
$this->{$belongs_to['column']}
// support missing combinations of operations
$foo()['bar']()
[$obj1, $obj2][0]->prop
getStr(){0}
// support nested ::
$foo['bar']::$baz
$foo::$bar::$baz
$foo->bar()::baz()
// support nested ()
foo()()
$foo->bar()()
Foo::bar()()
$foo()()
// support operations on arbitrary (...) expressions
(...)['foo']
(...)->foo
(...)->foo()
(...)::$foo
(...)::foo()
(...)()
// two more practical examples for the last point
(function() { ... })()
($obj->closure)()
// support all operations on dereferencable scalars
// (not very useful)
"string"->toLower()
[$obj, 'method']()
'Foo'::$bar
Unicode Codepoint Escape Syntax
echo "\u{202E}Right-to-left text";
echo " \u{26BD}";
Right-to-left text ⚽
ICU IntlChar class added to intl extension
CSPRNG
$int = random_int(-500, 500);
$bytes = random_bytes(10);
var_dump( $int );
var_dump( bin2hex($bytes) );
int(414) string(20) "f0491f72653eea697f0b"
void return type
function should_return_nothing(): void {
return 1; // Fatal error: A void function must not return a value
}
Nullable types
function answer(int $a, ?int $b): ?int {
if($a > $b) return $a;
else return null;
}
Iterable pseudo-type
function foo(iterable $iterable) {
foreach ($iterable as $value) {
// ...
}
}
function bar(): iterable {
return [1, 2, 3];
}
Negative string offsets
$str='abcdef';
var_dump($str[-2]); // => string(1) "e"
List keys
list($first, $second, $third) = [1, 2, 3];
[$first, $second, $third] = [1, 2, 3];
Warn about invalid strings in arithmetic
$numberOfApples = "10 apples" + "5 pears";
Notice: A non well formed numeric string encountered in example.php on line 2
Notice: A non well formed numeric string encountered in example.php on line 2
Class constant visibility
class Token {
// Constants default to public
const PUBLIC_CONST = 0;
private const PRIVATE_CONST = 0;
protected const PROTECTED_CONST = 0;
public const PUBLIC_CONST_TWO = 0;
Things that may break your code
It can catch dumb mistakes
$a = [1,2,3];
if(count($a > 1)) {
echo "Test";
}
% phan test.php
test.php:2 PhanTypeComparisonFromArray array to int comparison
Check phpdoc comments
class C {
/** @var int $prop */
public $prop;
/**
* @param string $arg
* @return int
*/
function test($arg) {
$this->prop = $arg;
return $arg;
}
}
% phan test.php
test.php:10 PhanTypeMismatchProperty Assigning string to property but \C::prop is int
test.php:11 PhanTypeMismatchReturn Returning type string but test() is declared to return int
Help with refactoring
class C {
/**
* @deprecated
*/
static function legacy_function() { }
}
C::legacy_function();
% phan test.php
test.php:8 PhanDeprecatedFunction Call to deprecated function \C::legacy_function() defined at test.php:5
Install with composer
$ composer require --dev etsy/phan
Create .phan/config.php
<?php
use \Phan\Issue;
return [
'should_visit_all_nodes' => true,
'minimum_severity' => Issue::SEVERITY_LOW,
'directory_list' => [ 'src', 'vendor' ],
'exclude_analysis_directory_list' => [ 'vendor' ]
];
$ ./vendor/bin/phan
Type Safety
Legacy code
class Data {
function __construct($data) {
$this->haystack = $data;
}
function find($needle) {
return in_array($needle, $this->haystack, true);
}
}
$storage = new Data(['apple','orange','banana']);
$fruit = false;
$storage->find($fruit);
Going straight to strict types risks runtime fatals
<?php declare(strict_types=1);
class Data {
function __construct(array $data) {
$this->haystack = $data;
}
function find(string $needle):bool {
return in_array($needle, $this->haystack, true);
}
}
$storage = new Data(['apple','orange','banana']);
$fruit = false;
$storage->find($fruit);
Fatal error: Uncaught TypeError: Argument 1 passed to Data::find() must be of the type string, boolean given,
called in test.php on line 13 and defined in test.php:6
Stack trace:
#0 test.php(13): Data->find(false)
#1 {main}
thrown in test.php on line 6
Intermediate step
class Data {
/** @var array $haystack */
public $haystack;
/**
* @param array $data
*/
function __construct($data) {
$this->haystack = $data;
}
/**
* @param string $needle
* @return bool
*/
function find($needle) {
return in_array($needle, $this->haystack, true);
}
}
$storage = new Data(['apple','orange','banana']);
$fruit = false;
$storage->find($fruit);
$ phan test.php
test.php:22 PhanTypeMismatchArgument Argument 1 (needle) is bool but \Data::find() takes string defined at test.php:15
Report Bugs
Useful bug reports, please!