Web Programming TutorialsLearn How to Use Generator Return Expressions and Delegation

Learn How to Use Generator Return Expressions and Delegation

Learn-How-to-Use-Generator-Return-Expressions-and-Delegation

In the last chapter, we discussed the various old extensions and SAPIs which are removed in PHP 7 and the new classes and interfaces which are added to PHP 7. In this chapter, we are going to discuss the new features of Generator Return Expressions and Generator Delegation which are added to PHP 7 upon the existing generator feature that was present in PHP 4 and 5.x. We will discuss about these new features of PHP 7 along with practical examples.

Generator Return Expressions
Generator return expression is a new feature that is built upon the existing generator functionality that was introduced in the PHP 5.5 version. With this feature, we can now use the return statement within a generator and are able to return the value of the final expression. Also, it is to be noted that we can only return the value of the expression but not the reference when using the Generator return expression. The value can be fetched with the use of new Generator:: getReturn () method that can only be used once the generator function has finished yielding the defined values.
It is a convenient ability to have, for being able to explicitly return a final value from a generator return expression. This is because PHP 7 enables for a final value to be returned by a generator from some form of co-routine computation in the program that could only be handled by the client code executing the generator. This is a simpler approach than forcing the client code to check first whether the final value has been yielded and then handle that value specifically.
Following is an example of Generator Return Expression.

<html>
   <head>
      <title><?php echo "Welcome to PHP-7 Tutorials";?></title>      
   </head>
   <body>
<?php
$generator = (function() {
	yield  "one";
	yield  "two";
	yield  "three";
	return "four";
})();

foreach ($generator as $val) {
	echo $val, PHP_EOL;
}

echo $generator -> getReturn(), PHP_EOL;
?>
   </body>
</html>

Explanation of PHP 7 code

  • Firstly open a PHP file in an editor and write the required HTML code as shown above and then in the body part of HTML inject the actual PHP 7 code for generator return expression.
  • In the above example, we have declared a function that has a reference as $generator.
  • In the function, with the reference as $generator, we have defined yield “one”, “two” and “three”.
  • Next, within the above function we are returning “four” as generator return expression.
  • Lastly, we are iterating on the “$generator” function till the end (PHP_EOL) and echoing the values of the yields along with the generator return expression.

Output
In the output, we will see the echo values starting 3 yield values and the last return expression value “one”, “two”, “three” and “four”. From the “$generator” function, we are iterating the 1st three yield and then the return expression until the end of PHP loop as shown below.
Img1
Generator Delegation
In PHP 7, the Generators can now automatically delegate to another generator, Traversable object or array and does not require to write the boilerplate in the outermost generator by using the yield from construct as in earlier versions of PHP.
Following is an example of Generator Delegation.

<html>
   <head>
      <title><?php echo "Welcome to PHP-7 Tutorials";?></title>      
   </head>
   <body>
<?php
function generator1()
{
    yield "one";
    yield "two";
    yield from generator2();
}
function generator2()
{
    yield "three";
    yield "four";
}
foreach (generator1() as $value)
{
    echo $value, PHP_EOL;
}
?>
   </body>
</html>

Explanation of PHP 7 code

  • Firstly open a PHP file in editor and write the required HTML code as shown above and then in the body part of HTML, inject the actual PHP 7 code for generator delegation.
  • In the above example, we have declared two functions known as “generator1” and “generator2”.
  • In the “generator1” function, we have defined yield “one”, “two” and “from generator2”.
  • In the “generator2” function, we have defined yield “three” and “four”.
  • Lastly, we are iterating on the “generator1” function till the end (PHP_EOL) and echoing the values of the yields.

Output
In the output, we will see the echo values starting yield “one”, “two”, “three” and “four”. From the “generator1” function, we are delegating to the “generator2” function, hence the following output.
Img2
Source code for Generator Return Expressions

Conclusion
In this chapter, we have discussed the new feature Generator Return Expressions and Generator Delegation which are added to PHP 7 based on the existing generator feature of PHP 4 and 5.x. In the upcoming chapter of this tutorial, we are going to discuss about changed functions, new Unicode codepoint escape syntax and new global constants which are added to PHP 7.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -