PHP och JavaScript

PHP 5.3

Namespaces


                        namespace Acme\Foo;

                        class Calculator {
                            public function ...
                        }
                    
src/Acme/Foo/Calculator.php
                        namespace Acme\Bar;

                        class Calculator {
                            public function ...
                        }
                    
src/Acme/Bar/Calculator.php

PHP 5.3

Closures


                    $values = array(1, 2, 3, 4, 5, 6, 7, 8);

                    $even = array_filter($values, function($value) {
                        return $value % 2 == 0;
                    });
                

                    $values     = array(1, 2, 3, 4, 5, 6, 7, 8);
                    $calculator = new Calculator();

                    $double = array_map(function($value) use ($calculator) {
                        return $calculator->multiply($value, 2);
                    });
                

PHP 5.3

Sorthand ternary operator


                    function doStuff($value) {
                        $value = $value ?: 'default';
                        
                        // ... 
                    }
                

PHP 5.3

DateInterval


                    $di = new DateInterval('P7D');
                    $date = new DateTime();
                    $date->add($di);
                

PHP 5.4

Traits


                    trait alphaTrait {
                        function alpha() {
                            return 'alpha'
                        }
                    }
                    
                    trait betaTrait {
                        function beta() {
                            return 'beta';
                        }
                    }

                    class First {
                        use alphaTrait;
                    }
                    
                    class Second {
                        use betaTrait;
                    }
                    
                    class Third {
                        use alphaTrait;
                        use betaTrait;
                    }
                

PHP 5.4

Array shorthand


                    $a = [1, 2, 3, 4];
                    $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
                

PHP 5.4

Function array dereferencing


                    class Repository {

                        /**
                         * @return array
                         */
                        function getPosts() {
                            // ...
                        }

                        function firstPost() {
                            return $this->getPosts()[0];
                        }
                    }
                

PHP 5.4

Class member access on instantiation


                    function unixtime() {
                        return (new DateTime())->format('u');
                    }
                

PHP 5.4

CLI Web Server


                    $ php -S localhost:8000
                

PHP 5.5

Generators


                function even($max = 10) {
                    $value = 0;
                    
                    while ($value <= $max) {
                        yield $value;
                        $value += 2;
                    }
                }
                
                $generator = even(8);
                
                foreach ($generator as $number) {
                    // 2, 4, 6, 8
                }
                

PHP 5.5

Try/catch/finally


                    try {
                        risky();
                    } catch (Exception $e) {
                        log('error', $e->getMessage());
                    } finally {
                        always();
                    }                    
                

PHP 5.5

foreach list()


                    // returns [ [ 'option', 'value' ], ... ]
                    $options = $settings->all();

                    foreach ($options as list($key, $value)) {
                        // ...
                    }
                

PHP 5.5

Password hashing API


                    password_hash('monkey', PASSWORD_DEFAULT);