4. ● release date: August 2014
● PHP5: last branch release
PHP5.6: Overview
5. PHP5.6: New features
● Constant expressions
const ONE = 1;
// Scalar Expression in constant
const TWO = ONE * 2;
● Variadic functions/Argument unpacking
function myTools($name, ...$tools) {
echo "Name:". $name.'<br />';
echo "My Tool Count:". count(tools);
}
myTools('Avinash', 'Eclipse');
myTools('Avinash', 'Eclipse', 'Sublime');
myTools('Avinash', 'Eclipse', 'Sublime', 'PHPStorm');
function myTools($name, $tool1, $tool2, $tool3) {
echo "Name:". $name.'<br />';
echo "Tool1:", $tool1.'<br />';
echo "Tool2:", $tool2.'<br />';
echo "Tool3:", $tool3;
}
$myTools = ['Eclipse', 'Sublime', 'PHPStorm'];
myTools('Avinash', ...$myTools);
6. PHP5.6: New features
● Exponentiation
// PHP5.5 and before
pow(2, 8); // 256
// PHP5.6 and after
echo 2 ** 8; // 256
echo 2 ** 2 ** 4; // 256
● use function and use const
namespace NameSpace {
const FOO = 42;
function f() { echo __FUNCTION__."n"; }
}
namespace {
use const NameSpaceFOO;
use function NameSpacef;
echo FOO."n";
f();
}
7. PHP5.6: New features
● phpdbg: The interactive debugger
http://phpdbg.com/docs
● php://input
// Now deprecated
$HTTP_RAW_POST_DATA
// This is now reusable
file_get_contents('php://input');
● Large file uploads
Files larger than 2 gigabytes in size are now accepted.
8. PHP5.6: New features
● __debugInfo()
class C {
private $prop;
public function __construct($val) {
$this->prop = $val;
}
public function __debugInfo() {
return [
'propSquared' => $this->prop ** 2,
];
}
}
var_dump(new C(42));
object(C)#1 (1) {
["propSquared"]=>
int(1764)
}
9. ● Release date: October 2015
● Engine: Zend v3 (32/64 bits)
● Performance: 25% to 70% faster
● Backward Incompatible Changes
● New Parser
● Tidy up old things
● Many new features
PHP7: Overview
10. ● Scalar Type Declarations
// must be the first statement in a file.
// If it appears anywhere else in the file it will generate a compiler error.
declare(strict_types=1);
// Available type hints
int, float, string, bool
// Examples
function add(string $a, string $b): string {
return $a + $b;
}
function add(int $a, int $b): int {
return $a + $b;
}
function add(float $a, float $b): float {
return $a + $b;
}
function add(bool $a, bool $b): bool {
return $a + $b;
}
PHP7: What's new
11. ● Group Use Declarations
// PHP7
use FooLibraryBarBaz{ ClassA, ClassB, ClassC, ClassD as Fizbo };
// Before PHP7
use FooLibraryBarBazClassA;
use FooLibraryBarBazClassB;
use FooLibraryBarBazClassC;
use FooLibraryBarBazClassD as Fizbo;
● switch.default.multiple
Will raise E_COMPILE_ERROR when multiple default blocks are found in a switch statement.
PHP7: What's new
12. ● Alternative PHP tags removed
<% // opening tag
<%= // opening tag with echo
%> // closing tag
(<scripts+languages*=s*(php|"php"|'php')s*>)i // opening tag
(</script>)i // closing tag
● Return Type Declarations
// Returning array // Overriding a method that did not
have a return type:
function foo(): array { interface Comment {}
return []; interface CommentsIterator extends
Iterator {
} function
current(): Comment;
}
// Returning object
interface A {
static function make(): A;
}
class B implements A {
static function make(): A {
return new B();
}
}
PHP7: What's new
13. ● Update json parser extension
json extension has been replaced by jsond extension
echo json_encode(10.0); // Output 10
echo json_encode(10.1); // Output 10.1
echo json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION); // Output 10.0
echo json_encode(10.1, JSON_PRESERVE_ZERO_FRACTION); // Output 10.1
var_dump(json_decode(json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION))); // Output double(10)
var_dump(10.0 === json_decode(json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION))); // Output bool(true)
● Removing old codes
PHP4 constructor
Removal of dead/unmaintained/deprecated SAPIs and extensions (imap, mcrypt, mysql, ereg,
aolserver, isapi, ...)
Remove the date.timezone warning
PHP7: What's new
14. ● new operators
o Nullsafe Calls
function f($o) {
$o2 = $o->mayFail1();
if ($o2 === null) {
return null;
}
$o3 = $o2->mayFail2();
if ($o3 === null) {
return null;
}
$o4 = $o3->mayFail3();
if ($o4 === null) {
return null;
}
return $o4->mayFail4();
}
function f($o) {
return $o?->mayFail1()?->mayFail2()?->mayFail3()?->mayFail4();
}
PHP7: What's new
16. ● new operators
o Null Coalesce
// Without null coalesce
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// With null coalesce
$username = $_GET['user'] ?? 'nobody';
PHP7: What's new
17. ● Context Sensitive Lexer
PHP7: What's new
Newly reserved Now possible
int Class::forEach()
float Class::list()
bool Class::for()
string Class::and()
true, false Class::or()
null Class::new()
Class::include()
const CONTINUE
18. ● Anonymous class support
/* implementing an anonymous console object from your framework maybe */
(new class extends ConsoleProgram {
public function main() {
/* ... */
}
})->bootstrap();
class Foo {}
$child = new class extends Foo {};
var_dump($child instanceof Foo); // true
// New Hotness
$pusher->setLogger(new class {
public function log($msg) {
print_r($msg . "n");
}
});
PHP7: What's new
19. ● Uniform Variable Syntax
// old meaning // new meaning
$$foo['bar']['baz'] ${$foo['bar']['baz']} ($$foo)['bar']['baz']
$foo->$bar['baz'] $foo->{$bar['baz']} ($foo->$bar)['baz']
$foo->$bar['baz']() $foo->{$bar['baz']}() ($foo->$bar)['baz']()
Foo::$bar['baz']() Foo::{$bar['baz']}() (Foo::$bar)['baz']()
// Before
global $$foo->bar;
// Instead use
global ${$foo->bar};
● Unicode Codepoint Escape Syntax
"U+202E" // String composed by U+ will now not be parsed as special characters
echo "u{1F602}"; // outputs 😂
● Instance class by reference not working anymore
$class = &new Class(); // Will now return a Parse error
PHP7: What's new
20. ● Loop else
foreach ($array as $x) {
echo "Name: {$x->name}n";
} else {
echo "No records found!n";
}
● Named Parameters
// Using positional arguments:
htmlspecialchars($string, double_encode => false);
// Same as
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
// non changing default value
htmlspecialchars($string, default, default, false);
PHP7: Proposed features/In draft
21. ● Union Types: multi types
function (array|Traversable $in) {
foreach ($in as $value) {
echo $value, PHP_EOL;
}
}
● Enum types
enum RenewalAction {
Deny,
Approve
}
function other(RenewalAction $action): RenewalAction {
switch ($action) {
case RenewalAction::Approve:
return RenewalAction::Deny;
case RenewalAction::Deny:
return RenewalAction::Approve;
}
}
PHP7: Proposed features/In draft