23
John Coggeshall @coogle http://www.coggeshall.org/ A PEEK AT PHP 7.0

Peek at PHP 7

Embed Size (px)

Citation preview

John Coggeshall

@coogle

http://www.coggeshall.org/

A PEEK AT PHP 7.0

WELCOME!• The CV

• PHP Core contributor (author of ext/tidy)

• PHP Developer since 96’

• Published lots of things

• Spoken lots of places

Melted Metal!

Love the boat!

ALL THE THINGS : PHP 7• PHP 7 is the next major iteration of the PHP language

• Awesome new language features

• More sanity / consistency

• More speed!

• But wait, what happened to PHP 6?

• PHP 6 is dead.

• The short version: PHP 6’s big push was going to be Unicode support, but there wasn’t enough developers to get that done so the tree stagnated and died.

• When will PHP 7 be released?

• Best guess: GA Fall, 2015

WarningEverything I say here could be wrong.

… It’s probably not

… A lot of it is already implemented

… I probably got the date wrong

… There aren’t RFCs for everything

… At least I didn’t write a book on PHP 6

ERROR HANDLING

THE DEATH OF E_STRICT• E_STRICT was our way of telling developers they were doing it wrong ™

• In PHP 7, E_STRICT is gone, and anything that was E_STRICT has been upgraded to one of the following:

• Nothing (we just removed the E_STRICT error because it was dumb)

• Make it an E_DEPRECATED error (probably should have ran with this from the beginning)

• Promote it to an E_NOTICE or E_WARNING (we’re serious about not letting you do these things, knock it off)

Oh, Yeah! Hell, No.28 4

https://wiki.php.net/rfc/reclassify_e_strict

CALL TO MEMBER FUNCTION OF…ARG!!• A really common error in PHP 5+ is “Call to member function blah() of non object”

• Basically, you tried to call a method on something that wasn’t an object (typically NULL)

• $foo->bar->baz();

• In PHP 5, this is a fatal error and your application blows up

• In PHP 7 this has been changed to an E_RECOVERABLE_ERROR making it something you can, you know, recover from in your application

Oh, Yeah! Hell, No.32 0

https://wiki.php.net/rfc/catchable-call-to-member-of-non-object

ENGINE EXCEPTIONS• PHP 7 introduces the concept of using Exceptions for engine-level error handling to

replace E_ERROR, etc.

• Many / Most common error conditions will be converted to exceptions

• Future error conditions will, if possible, be implemented as exceptions

• Also introduced a revamped Exception object model

• BaseException

• EngineException

• ParseException

• Exception

• …

• Note: Existing catch code will not catch these new exceptions (clever)

Oh, Yeah! Hell, No.60 2

Oh, Yeah! Hell, No.39 19

https://wiki.php.net/rfc/engine_exceptions_for_php7

OBJECTS ‘N STUFF

WAIT.. THIS WAS BROKEN??• In current versions of PHP, some internal classes could fail in the constructor and return a

object that, for all intensive purposes, was broken.

• In PHP 7, all internal classes throw exceptions in their constructors to prevent broken instances

<?php// Reflection function is not meant to accept an array (E_WARNING)$reflection = new ReflectionFunction([]);// Blows upvar_dump($reflection->getParameters());

Oh, Yeah! Hell, No.

32 1

https://wiki.php.net/rfc/internal_constructor_behaviour

SPEAKING OF CONSTRUCTORS..• Also in PHP 7, the PHP 4 (circa 2000 – 15 years old) style of object constructors having

the same name as the class are one step closer to dying to the relief of all serious developers.

• Methods named the same thing as the class (i.e. Filter::filter()) are just methods now

• Constructors are called __construct()

• In PHP 7 if it looks like you are using this horrible behavior it will now yell at you with an E_DEPRECIATED

Oh, Yeah! Hell, No.50 4

https://wiki.php.net/rfc/remove_php4_constructors

SIMPLIFIED USE STATEMENTS• In PHP 7 we have added the notion of use statement “groups”

• Cleans up the top of our scripts

use Foo\Bar\ClassA;use Foo\Bar\ClassB;use Foo\Bar\ClassC;

use Foo\Bar\{ClassA, ClassB, ClassC};

https://wiki.php.net/rfc/group_use_declarations

Oh, Yeah! Hell, No.

39 19

LANGUAGE FEATURES

SPACESHIPS!

https://wiki.php.net/rfc/combined-comparison-operator

• PHP 7 Introduces the “Combined Comparison Operator” (a.k.a. the Spaceship operator)

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

Oh, Yeah! Hell, No.

43 11

?? OPERATOR• No, that wasn’t a placeholder in my slides

• The ?? Operator, or Null Coalesce Operator which allows us to set default values for variables when the variable doesn’t exist.

• Cleaner than trying to use ?: or other approaches

https://wiki.php.net/rfc/isset_ternary

Oh, Yeah! Hell, No.

31 3

<?php// Useful for when you want to set default values for variables$user = $_GET[‘user’] ?? ‘nobody’;

RETURN TYPES• My personal war over the years has included this one

• Return type-hints will finally be part of PHP in PHP 7

• Doesn’t support “nullable” types (yet)

<?php// Useful for when you want to set default values for variablesfunction returnsArray(): array { return [‘happy’, ‘days’];}

Oh, Yeah! Hell, No.

47 3

https://wiki.php.net/rfc/return_types

UNIFORM VARIABLE SYNTAX• PHP has some… complicated|odd|dumb|insane variable syntax possibilities

• $foo->$bar[‘baz’]()

• There are a lot of inconsistencies in the way we resolve variables when they get a little crazy

• In PHP 7, we are introducing a uniform syntax and fixing all of those issues

https://wiki.php.net/rfc/uniform_variable_syntax

Oh, Yeah! Hell, No.

30 1

<?php$foo()[‘bar’](); // Call closure in array from return value[$obj1, $obj2][0]->prop; // Access $obj1->propreturnString(){0}; // Return first character

$foo::$bar::$baz; // Stacked static calls$foo->bar()::baz(); // Stacked mixed calls

$foo()(); // Stacked function calls$foo->bar()(); // Stacked method calls

WE STILL HAD THAT?• Arcane open tags for PHP scripts are no longer supported in PHP 7

• ASP-style <% %>

• HTML-style <script language=“php”></script>

• Also, Hexadecimal support for “numeric strings” has been removed

• Before: ‘0x01’ == ‘1’ (true)

• Before: (int)’0x01’ == (int)’1’ (false, wtf?)

• Now: ‘0x01’ == ‘1’ (false)

Oh, Yeah! Hell, No.26 8

Oh, Yeah! Hell, No.29 0

https://wiki.php.net/rfc/remove_hex_support_in_numeric_strings

https://wiki.php.net/rfc/remove_alternative_php_tags

SCALAR TYPE HINTS• PHP 7 will have support for scalar type hinting in addition to the already supported

complex-type hinting

• Introduces the notion of declare(strict_types=1) directives

• Per-file directive

• Put at the top of the file, will enforce strict typing of return values

• Caller file counts, not source file

Oh, Yeah! Hell, No.108 48

https://wiki.php.net/rfc/scalar_type_hints_v5

<?phpdeclare(strict_types=1);

function add(float $a, float $b) : float { return $a + $b;}

add(1, 2); // retval = float(3)

NEW LIST() BEHAVIOR• The list() statement(?) is a odd thing in PHP used to extract array values into variables

• list($a, $b, $c) = [1, 2, 3]; // $a = 1, $b = 2, $c = 3

• In PHP 7 there are a couple of changes to the behavior

• list() now assigns variables left-to-right instead of right-to left

• list() no longer supports extracting values from strings

<?php$a = []list($a[], $a[], $a[]) = [1, 2 ,3];

// OLD: $a = [3, 2, 1];// NEW: $a = [1, 2, 3];

https://wiki.php.net/rfc/abstract_syntax_tree

https://wiki.php.net/rfc/fix_list_behavior_inconsistency

GO JOHNNY, GO!• The so-called PHPNG

branch of PHP has been merged as the basis for the engine in PHP 7

• Translation: PHP most of the time is a lot faster than before

• One step closer to effective meaningful JIT for PHP

Oh, Yeah! Hell, No.47 2

https://wiki.php.net/rfc/phpng

KEEP UP TO DATE WITH THE LATEST• PHP 7 like all things is going to be a moving target until it is done

• There is a VirtualBox of PHP 7 to toy around with maintained by Rasmus Lerdorf

• https://github.com/rlerdorf/php7dev

• The PHP RFC Wiki is also a great source of more details on everything we’ve discussed and the things currently being considered

• https://wiki.php.net/rfc

John Coggeshall

@coogle

http://www.coggeshall.org/

THANK YOU / QUESTIONS?