June 8, 2016 by Christoff Truter PHP
Originally I was planning to do a little write-up about the latest syntax introduced into PHP 7.0.x, but then I noticed quite a significant number of syntax changes leading up to this version, many of which I was unaware of since I last used PHP in version 5.3.x.
So if you fell asleep (like me), here is a brief list of syntax changes (I could find) since PHP 5.3.x, I am not going to go into too much depth about these changes, think about this as a crash course.
Classes
class LatLng { public static $lat = -25.7313; public static $lng = 28.2184; } $x = "LatLng"; echo $x::$lat;
class SomeBaseClass { public static function Context() { echo __CLASS__; } public static function UsingLateBinding() { static::Context(); }This is all about scope, the self keyword resolves to the class in which it is being implemented ignoring context / inheritance, while a statically invoked function will take context / inheritance into account.
public static function UsingSelf() { self::Context(); } } class SomeDerivedClass extends SomeBaseClass { public static function Context() { echo __CLASS__; } } SomeDerivedClass::UsingLateBinding(); SomeDerivedClass::UsingSelf();
trait Logging { private function Write($message) { /* Do Something */ } } class SomeClass { use Logging; public function __construct() { $this->Write('Something'); } } class SomeOtherClass { use Logging; public function __construct() { $this->Write('Something'); } }
$a = (new SomeClass)->SomeMethod();
class SomeClass { public static function SomeMethod() { echo "Output"; } } SomeClass::{"SomeMethod"}();
namespace CSTruter; class SomeClass { } echo SomeClass::class;
function SomeFunction($c) { $c->SomeMethod(); } SomeFunction( new class { public $Name = 'apple'; public function SomeMethod() { echo $this->Name; } });
Namespaces (5.3.x)
One of the most important changes in PHP was the introduction of namespaces, thereby solving name collisions (ambiguity) issues and giving us the ability to alias names, improving readability and reusability of source code.
Combination Syntax
namespace CSTruter;
const SOMECONSTANT = 1;
class SomeClass {}
function SomeFunction() {}
namespace CSTruter\MVC;
const SOMECONSTANT = 2;
class SomeClass {}
function SomeFunction() {}
namespace CSTruter\MVC {
const SOMECONSTANT = 1;
class SomeClass {}
function SomeFunction() {}
}
namespace CSTruter {
const SOMECONSTANT = 2;
class SomeClass {}
function SomeFunction() {}
}
namespace { // Global Namespace
const SOMECONSTANT = 3;
class SomeClass {}
function SomeFunction() {}
}
echo SOMECONSTANT;
echo \CSTruter\SOMECONSTANT;
echo \CSTruter\MVC\SOMECONSTANT;
use CSTruter\SomeClass;
use CSTruter\MVC\SomeClass as SomeOtherClass;
use CSTruter\SomeClass,
CSTruter\MVC\SomeClass as SomeOtherClass;
Or using Group use declarations (7.0.x)
use CSTruter\Example\{SomeClass,SomeOtherClass};
use function / use const (5.6.x)
use const CSTruter\MVC\SOMECONSTANT;
use function CSTruter\MVC\SomeFunction;
Constants
const SOMECONSTANT = 1; class SomeClass {}
const SOMECONSTANT = E_ERROR | E_WARNING | E_PARSE;
const SOMECONSTANT = [1, 2, 3];
Operators
$x = null; $y = null; echo $x ?: $y ?: "Hello";
echo 2 ** 3;
function SomeFunction($value, ... $values) { print_r($values); echo func_num_args(); // outputs 4, not cool } SomeFunction(1, 2, 3, 4);
function SomeFunction($x, $y, $z) { echo $x + $y + $z; }
$args = [1, 2, 3];
SomeFunction(...$args);
echo 1 <=> 1; // 0 echo 2 <=> 1; // 1 echo 1 <=> 2; // -1
$a = null; $b = ["value" => 1]; echo $a ?? $b["value1"] ?? $b["value"]; echo $a ?: $b["value1"] ?: $b["value"]; // Undefined index: value1
function SomeMethod(int $key, string $value) { }
function SomeMethod(float $value) : bool { }
Exceptions
function doSomething() { try { throw new Exception("Something went wrong"); } catch(Exception $ex) { throw new Exception("Something else went wrong", 0, $ex); } } try { doSomething(); } catch(Exception $ex) { print_r($ex); }
try { throw new Exception('Something wrong'); } finally { echo 'Still Do This'; } echo 'Something to do';
Dereferencing
function SomeMethod() {
return ['Apple', 'Orange', 'Lemon'];
}echo SomeMethod()[1];
echo ['The', 'Rain', 'In', 'Spain'][2];
echo 'The rain in spain'[2];
$a = function($value) {
return $value;
};echo $a('SomeValue');
goto SomeSection;
echo 'Unreachable Code';SomeSection:
echo 'Something';
echo 0b101;
$fruit = ['Apples', 'Oranges', 'Bananas'];
function SomeFunction() { yield 'Apples'; yield 'Oranges'; yield 'Bananas'; echo '2'; } echo '1'; $generator = SomeFunction(); echo '3'; foreach($generator as $item) { // Do Something }
function SomeFunction() { yield 'Apples'; yield 'Oranges'; yield from SomeOtherFunction(); yield 'Bananas'; yield from ['Apricots', 'Grapes']; } function SomeOtherFunction() { yield 'Pineapple'; yield 'Strawberries'; }
Additional Reading
Migrating from PHP 5.2.x to PHP 5.3.x
Migrating from PHP 5.3.x to PHP 5.4.x
Migrating from PHP 5.4.x to PHP 5.5.x
Migrating from PHP 5.5.x to PHP 5.6.x
Migrating from PHP 5.6.x to PHP 7.0.x