4. Desember 2013, TechShock
Does search on Solr and Lucene, PHP and python for Amedia.
Just passed 15 years as a PHP developer (since PHP 3.0.3-ish).
<?php
namespace foo;
class Bar {} // foo\Bar
<?php
require "foo.php";
use foo as baz;
$o = new baz\Bar();
<?php
class A {
public static function who() { echo __CLASS__; }
public static function test() {
self::who(); static::who();
}
}
class B extends A {
public static function who() { echo __CLASS__; }
}
B::test(); // AB
$a = "foo";
$a::factory();
Makes functions first class members
$foo = function ($a) {
return $a*2;
}
$foo(8); // 16
$var = 32;
$foo = function ($a) use ($var)
{
return $a * $var;
}
$foo(8); // 256
Fixes potential memory leaks with arrays containing other arrays, etc.
The weapon of choice for PHP on nginx in particular. Spawns a pool of FastCGI workers / processes that handles requests as they come in.
<?php
trait HelloWorlder {
function hello() { echo "Hello World!\n"; }
}
class Welcomer {
use HelloWorlder;
}
(new Welcomer())->hello();
<?php
$a = ['foo', 'bar', 'baz'];
$d = ['foo' => 'bar', 'baz' => 'eggs'];
<?php
function foo() {
return array('bar', 'baz', 'eggs', 'spam');
}
echo foo()[2]; // eggs
echo array('foo', 'bar', 'baz')[0]; // foo
echo ['foo', 'bar'][1]; // bar
<?= $foo ?> // <?php echo $foo; ?>
php -S localhost:8000 -t foo/ router.php
<?php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
return false; // serve the requested resource as-is.
}
echo "Insert default value here.";
<?php
class A {
static public function bar() {}
public function baz() {}
}
function spam() {}
function foo(Callable $foo) { $foo(); }
foo(array("A", "bar")); // works
foo([new A(), "baz"]); // works
foo("spam"); // works
foo(function () {}); // works
foo(14); // fatal error
foo("eggs"); // fatal error
Provides secure password hashing using BCRYPT and proper salting by default.
$hash = password_hash($password, PASSWORD_BCRYPT);
if (password_verify($password, $hash)) {
Use password_compat
on PHP < 5.5.
<?php
function square($value) {
while (true) {
$value *= 2;
yield $value;
}
}
foreach (square(2) as $val) {
if ($val > 256) break;
var_dump($val); // 4 8 16 32 64 128 256
}
APC more or less dead; use APCu for in-memory cache if needed.
<?php
namespace foo;
class A {}
echo A::CLASS; // foo\A
$foo ?: "default";
__callStatic(), __invoke()
array('ClassName', 'Foo'), $closure, [$obj, 'method']
if (empty(trim($foo))) // if (trim($foo))
foreach ($arr as list($el1, $el2))
A common protocol for how to map class names to actual file paths.
spl_autoload_register(function($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
});
Common coding standards across frameworks.
Replaces the old PEAR coding standards / Zend / etc.
Logging interface
Small changes to avoid a namespaced class mapping to the same file as a class name with _ as separators.
Foo\Bar\Baz
vs Foo_Bar_Baz
Shorter path mapping, more suitable for composer-style distribution.
vendor/foo/bar/src/Baz
.. which leads us into ..