For easy using new features and functionality Part 1.
It was created for the fastest way to switch and adapt PHP world for new version.
Let's change PHP world together!
php7_EasyWay require php 7 and php 5.6 for testing.
You can upload it from php.net and instal php7 manualy, have a look how:
But if you are lazy programmer and don't want to do all job by yourself, you can use PHP stack for:
spaceship "<=>" Full example
/*
* sort function, if first is bigger than second return -1, else return 1, or 0 if it's equals
*/
usort($names, function($a, $b) {
return [$a[1], $a[0]] <=> [$b[1], $b[0]];
});
Nullcoalesce operator "??" Full example | form for example
/*
* return first not null value
*/
$name = $_POST['name'] ?? 'guest';
// Four-digit codepoints
echo "<p>Caf\u{00e9} Royal</p>";
$tel = "\u{260E}";
echo "<p>$tel (212) 555-0199</p>";
// Five-digit codepoint
echo "<p>PHP 7 has spaceships! \u{1F680}</p>";
// Leading zeros omitted
echo "<p>Caf\u{e9} Royal</p>";
// JSON not affected
echo json_decode("\"\u00e9\"");
bin2hex(random_bytes(20));
New yield: return operator | delegate generator
Now we have new operator return, that we can use for some final value, it's actual for ending iteration. We also can use it for testing, and be able to know about correct parsing of yield.
Also yields can return some value from other yields, in this way we can create more complicated operations that is based on simple ones.
function listA() {
yield 2;
yield 3;
yield 4;
}
function listB() {
yield 1;
yield from listA(); // 'listA' is working here
yield 5;
return 'success'; // final result, that we can check out later
}
foreach (listB() as $val) {
echo "\n $val"; // output values from 1 to 5
}
$genB()->getReturn(); // return 'success' if there are no errors
Scalar parameter hints: Full example | Interface for example
- Mode in definition file is irrelevant
- Mode is determined by file that calls the function
- Weak mode in file that calls the function.
Parameters are cast to specified data type - Strict mode in file that the function.
Parameters must match the specified type
function say(string $word) : string {
return "Hello, " . $word
}
Scalar type hints: Full example | strict mode for example
- Parameter type hints
Mode is determined by the file calling the function - Return type declarations
Mode is determined by the file that defines the function - Weak mode casts values to the specified type
- Strict mode enforces use of the specified type
Worked by the function was defined, but no where function is gone.
declare(strict_types=1);
Perform a regular expression search and replace using callbacks
preg_replace_callback_array([$pattern => function($matches){}
[, $pattern => function($matches){}]
], $list);
Example:
preg_replace_callback_array(
[
'~[a]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
},
'~[b]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
}
],
$subject
);
Result:
6 matches for "a" found
3 matches for "b" found
Anonymous classes: Full example
- Syntax is identical to a named class, but without a name
- Can be assigned to a variable, returned from a function, or passed as an argument to a function
Uses:
- When the class does not need to be documented
- Single-use class
- Unit testing
For more improved programmers: Delegate and Return generator
New namespace using: Full example
use Symfony\Component\Console\{
Helper\Table,
Input\ArrayInput,
Input\InputInterface,
Output\NullOutput,
Output\OutputInterface,
Question\Question,
Question\ChoiceQuestion as Choice,
Question\ConfirmationQuestion,
};
use app\classes\common{
DoSomething,
const PIE,
const LOCATION,
function doubleIt,
function cube
};
Also you can read about new errors in php 7, as for me it's become more obviously: Errors
In addition read wikiPHP and manual for better understanding.
/*
* Example of an IIFE (immediately invoked function expression).
* This is a construction familiar from JavaScript when you don't
* want to pollute the global scope with variables or objects.
*/
(function($name = null) {
$name = $name ?? 'stranger';
$person = new class($name)
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function greet()
{
echo 'Hi, ' . $this->name;
}
};
$person->greet();
})();
$greet = function() {
return [
'welcome' => function() {
echo 'Hi, there!';
},
'farewell' => function() {
echo 'So long!';
}
];
};
$greet()['welcome']();
class Person
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
$person1 = new Person('David');
$person2 = new Person('Adam');
$person = [$person1, $person2][1];
echo $person->name;
//echo [$person1, $person2][1]->name;
Uniform variable syntax: Example
- Internally consistent way of interpreting variables
- Opens up wide range of new structures
- Immediately invoked function expression(IIFE)
- Variable variables
Backward incompatible changes affect some edge cases
Simple variable variables are unaffected
You can read about it here
PHP 7 is faster, cleaner and greener $$$!
- Up to 3x increased perfomance
- Outdated features removed
- Greater internal consistency
- Little backward compatibility breakage
- Handling of fatal errors
- Simplified debugging with assert()
- Anonymous classes
- Simplified binding to closures
- New operators
Spaceship(combined comparison)
Null coalesce
- Generator delegation--coroutines
- Increased deferencing possibilities
- Immediately invoked function expressions