PHP
I've been using php for over 10 years. it becomes better and better.
I also learned some other lanugages, php remains one of my favourite language.
You want your own aws lambda and api gateway? simply upload a php script and you've got it.
PHP 7
Return type declarations
function arraysSum(array ...$arrays): array
turn on strict types by declare(strict_types=1);
(usually at the first line like <?php declare(strict_types=1);
)
too java?? I think it's a good thing. optional typed is a big win IMHO.
Null coalescing operator
no more isset()
checks
$username = $_GET['user'] ?? 'nobody';
define() constant arrays
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
Anonymous classes
WTF?? I hate it.
Group use declarations
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
Generator::getReturn()
read the documentation
PHP 5.6
variadic functions
function f($req, $opt = null, ...$params) {
...
}
Argument unpacking via ...
$operators = [2, 3];
echo add(1, ...$operators);
use function and use const
namespace {
use const Name\Space\FOO;
use function Name\Space\f;
...
}
PHP 5.5
generator
read the documentation
when you code arrives yield
, it returns and suspends, so it is like lazy
finally
try ... catch ... finally
password_hash()
the correct way to handle password hashing, read the documentation
PHP 5.4
Short array syntax
like javascript, $a = [1, 2, 3, 4];
Class member access on instantiation
one of my favourite change: (new Foo)->bar()
built-in web server
read the documentation
traits
(the reason to use trait
instead of static
class methods, is you can access private
/ protected
properties / methods.)
trait Hello {
public function sayHello() {
echo 'Hello ';
}
}
trait World {
public function sayWorld() {
echo 'World';
}
}
class MyHelloWorld {
use Hello, World;
public function sayExclamationMark() {
echo '!';
}
}
$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
$o->sayExclamationMark();
closures
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
Composer
use composer, seriously.
Editor
use netbeans, seriously.
Quick note
detect cli mode
if (php_sapi_name() == "cli") {
// runs under cli
}