Table of contents● Preamble● PHP5.6: in case you missed something
a. Overviewb. New features
● PHP7: future is cominga. Overviewb. What's new
Preamble● Use filter_var()
// Check valid emailbool filter_var('[email protected]', FILTER_VALIDATE_EMAIL);
// Remove empty values$data = array( 'value1' => '', 'value2' => null, 'value3' => []);filter_var_array($data); // return empty array
● Speed the codeDON'T open/close PHP tags for excessive.
DON'T use GLOBALS
PHP5.6: New features● Constant expressions
const ONE = 1;// Scalar Expression in constantconst TWO = ONE * 2;
● Variadic functions/Argument unpackingfunction 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);
PHP5.6: New features● Exponentiation
// PHP5.5 and beforepow(2, 8); // 256
// PHP5.6 and afterecho 2 ** 8; // 256echo 2 ** 2 ** 4; // 256
● use function and use constnamespace Name\Space { const FOO = 42; function f() { echo __FUNCTION__."\n"; }}
namespace { use const Name\Space\FOO; use function Name\Space\f;
echo FOO."\n"; f();}
PHP5.6: New features● phpdbg: The interactive debugger
http://phpdbg.com/docs
● php://input// Now deprecated$HTTP_RAW_POST_DATA
// This is now reusablefile_get_contents('php://input');
● Large file uploadsFiles larger than 2 gigabytes in size are now accepted.
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)}
● 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
● 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 hintsint, float, string, bool
// Examplesfunction 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
● Group Use Declarations// PHP7use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo };
// Before PHP7use FooLibrary\Bar\Baz\ClassA;use FooLibrary\Bar\Baz\ClassB;use FooLibrary\Bar\Baz\ClassC;use FooLibrary\Bar\Baz\ClassD as Fizbo;
● switch.default.multiple
Will raise E_COMPILE_ERROR when multiple default blocks are found in a switch statement.
PHP7: What's new
● Alternative PHP tags removed<% // opening tag<%= // opening tag with echo%> // closing tag(<script\s+language\s*=\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 objectinterface A { static function make(): A;}class B implements A { static function make(): A { return new B(); }}
PHP7: What's new
● Update json parser extension
json extension has been replaced by jsond extensionecho json_encode(10.0); // Output 10echo json_encode(10.1); // Output 10.1echo json_encode(10.0, JSON_PRESERVE_ZERO_FRACTION); // Output 10.0echo json_encode(10.1, JSON_PRESERVE_ZERO_FRACTION); // Output 10.1var_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
● new operatorso 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
● new operatorso Spaceship
PHP7: What's new
operator <=> equivalent
$a < $b ($a <=> $b) === -1
$a <= $b ($a <=> $b) === -1 || ($a <=> $b) === 0
$a == $b ($a <=> $b) === 0
$a != $b ($a <=> $b) !== 0
$a >= $b ($a <=> $b) === 1 || ($a <=> $b) === 0
$a > $b ($a <=> $b) === 1
● new operatorso Null Coalesce
// Without null coalesce$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// With null coalesce$username = $_GET['user'] ?? 'nobody';
PHP7: What's new
● Context Sensitive LexerPHP7: 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
● 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
● 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']()
// Beforeglobal $$foo->bar;// Instead useglobal ${$foo->bar};
● Unicode Codepoint Escape Syntax"U+202E" // String composed by U+ will now not be parsed as special charactersecho "\u{1F602}"; // outputs 😂
● Instance class by reference not working anymore$class = &new Class(); // Will now return a Parse error
PHP7: What's new
● Loop elseforeach ($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 ashtmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);// non changing default valuehtmlspecialchars($string, default, default, false);
PHP7: Proposed features/In draft
● Union Types: multi typesfunction (array|Traversable $in) { foreach ($in as $value) { echo $value, PHP_EOL; }}
● Enum typesenum 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
PHP7: References● PHP: rfc
● Etat des lieux et avenir de PHP
● En route pour PHP7
Top Related