php 7 – generator – yield’s

php 7 has now implement generators where you are able to return (yield) results from a method and only the ones that you will need.

// define the output as type Generator -- e.g. yield results.. the compiler will still work without this Generator keyword
function yieldSomeNumbers() : Generator {  
    yield 10;
    yield 13;
}
 
foreach (yieldSomeNumbers() as $v) {
    var_dump($v);
}

Will output

10
13

The ‘Generator’ at the end of the method ” : Generator” is not actual needed as the compiler will append it as such on the fly since the method is yield results.

For example, lets say that you are doing a search of numbers from 1-100 and are searching for the value of 10, so before generators the code would have been something like

function generateNumbers() {
    return range(1,100);   // load up a array of values 1-100 e.g. 1,2,3,4,5...
}
 
foreach (generateNumbers() as $v){
    var_dump($v);
    if ($v == 10) {
        var_dump("FOUND");
        break;
    }
}

The ‘foreach (generateNumbers()’ will be using the full array where as

function generateSomeNumbers() : Generator {
    foreach (range(1,100) as $v) {
        yield $v;
    }
}
 
foreach (generateSomeNumbers() as $v){
    var_dump($v);
    if ($v == 10) {
        var_dump("FOUND");
        break;
    }
}

will only return each yield upon request.

php 7 – strict typing and return types

This is going to be the first of my PHP 7 posts where PHP 7 has grown up and has PHP 7 taken a massive leap forwards!!!

So the first thing is return types, before PHP 7 there was no return typing e.g.

function add($a,$b) {
    return $a + $b;
}
 
echo add("hi there","good");

was a “valid” PHP code!!

But now with not only parameter type hinting, you are also able to use return types with strict typing as well!. So to start with, this is defining the return type of int (just before the function body there is the :int)

function add(int $a,int $b): int {
    return (int)($a + $b);
}
 
echo add(2,3);

The answer of course will be 5, but you could still be silly and allow PHP to convert string characters (that was alphanumeric) to integers, for example calling the above function like

echo add("2","3");

Would still work and give the answer of 5!, which is kinder wrong!.

So there is the strict typing which enforces the correct parameter being used.

So if you change the above code to

declare(strict_types=1);
 
 
function add(int $a,int $b): int {
    return (int)($a + $b);
}
 
 
var_dump(add(2,3));
var_dump(add("2","3"));
exit;

The output would be

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to add() must be of the type integer, string given, called in /home/genux/Programming/php7/scalar.php on line 12 and def
ined in /home/genux/Programming/php7/scalar.php:5
Stack trace:
#0 /home/genux/Programming/php7/scalar.php(12): add('2', '3')
#1 {main}
  thrown in /home/genux/Programming/php7/scalar.php on line 5

Which means that you have pass in the integer values, as in strict typing and not just assume PHP will be “cool” and just sort it out for you.