Skip to content

sydorenkovd/php7_EasyWay

Repository files navigation

php7 guide 🚀

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!

REQUIREMENTS

php7_EasyWay require php 7 and php 5.6 for testing.

How instal php 7 on your local server: 🐘

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:

DOCUMENTATION

New operators and functions:

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';
Unicode
// 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\"");
generate random string
bin2hex(random_bytes(20));

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

Type checking:

Scalar parameter hints: Full example | Interface for example
  1. Mode in definition file is irrelevant
  2. Mode is determined by file that calls the function
  3. Weak mode in file that calls the function.
    Parameters are cast to specified data type
  4. Strict mode in file that the function.
    Parameters must match the specified type
Compound return type and typeError in error case:
function say(string $word) : string {
return "Hello, " . $word
}
  1. Parameter type hints
    Mode is determined by the file calling the function
  2. Return type declarations
    Mode is determined by the file that defines the function
  3. Weak mode casts values to the specified type
  4. Strict mode enforces use of the specified type
Turn on strict mode

Worked by the function was defined, but no where function is gone.

declare(strict_types=1);
preg_replace_callback_array

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

Classes and generators:

Anonymous classes: Full example
  1. Syntax is identical to a named class, but without a name
  2. 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
};

Errors


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.


Other changes:

/*
 * 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();
})();
Dereferencing a return value, then calling it.
$greet = function() {
    return [
        'welcome' => function() {
            echo 'Hi, there!';
        },
        'farewell' => function() {
            echo 'So long!';
        }
    ];
};
$greet()['welcome']();
Dereferencing an array of objects to get a property.
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

  1. Internally consistent way of interpreting variables
  2. Opens up wide range of new structures
  3. Immediately invoked function expression(IIFE)
  4. Variable variables
    Backward incompatible changes affect some edge cases
    Simple variable variables are unaffected

You can read about it here

Сonclusion

PHP 7 is faster, cleaner and greener $$$!

  • Up to 3x increased perfomance
  • Outdated features removed
  • Greater internal consistency
  • Little backward compatibility breakage

Improved features:

  • Handling of fatal errors
  • Simplified debugging with assert()
  • Anonymous classes
  • Simplified binding to closures
  • New operators
    Spaceship(combined comparison)
    Null coalesce

A forward-looking version

  • Generator delegation--coroutines
  • Increased deferencing possibilities
  • Immediately invoked function expressions

I hope you founded it useful and will develop using PHP 7 in your projects.

Write code and enjoy process, X 🌟

About

Faster, cleaner, greener!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages