Introduction
Iterating over a large amount of data using a loop structure (such as foreach) will require a lot of memory and considerable processing time. Use Generators to iterate over a set of data without this overhead. Generator functions are like ordinary functions. However, instead of a return statement in the function, the generator is executed repeatedly using the yield keyword to provide the values to be iterated over.
The yield keyword is the core of the generator mechanism. Although its usage looks similar to return, it does not stop function execution. It provides the next value of the iteration and pauses the execution of the function.
Syntax
Generator implements Iterator { /* Methods */ public current ( void ) : mixed public getReturn ( void ) : mixed public key ( void ) : mixed public next ( void ) : void public rewind ( void ) : void public send ( mixed $value ) : mixed public throw ( Throwable $exception ) : mixed public valid ( void ) : bool public __wakeup ( void ) : void }
Method
public Generator::current (void) − mix — Get the generated value
public Generator::getReturn ( void ) : mix — Get the return value of the generator
public Generator::key ( void ) − mix — Get the key of the generated value.
p>public Generator::next ( void ) − void — Resumes execution of the generator. The effect is the same as calling Generator::send() with NULL as argument.
public Generator::rewind ( void ) − void — Rewind the iterator. This will throw an exception if the iteration has already started.
public Generator::send (mixed $value) : mix - Sends the given value to the generator as the result of the current yield expression and restores the generator.
public Generator::throw ( Throwable $exception ) − mix — Throws an exception into the generator and resumes execution of the generator.
public Generator::valid ( void ) − bool — Check if the iterator has been closed
public Generator::__wakeup ( void ) − void — Exception thrown because the generator cannot be serialized.
The Generator class implements the Iterator interface. Generator objects cannot be instantiated via new. Any user-defined function with the yield keyword creates an object of the generator class.
Generator Example
Since the generator implements the Iterator interface, each loop can be used to iterate over the generated values.
Live demonstration
<?php function squaregenerator(){ for ($i=1; $i<=5; $i++){ yield $i*$i; } } $gen=squaregenerator(); foreach ($gen as $val){ echo $val . " "; } ?>
Output
The above program displays the following output
1 4 9 16 25
The following example uses the current() and next() methods of the generator class to Traverse the generated values. Use the valid() method to check loop conditions.
Example
Real-time demonstration
<?php function squaregenerator(){ for ($i=1; $i<=5; $i++){ yield $i*$i; } } $gen=squaregenerator(); while ( $gen->valid() ){ echo "key: " . $gen->key(). " value: ". $gen->current() . ""; $gen->next(); } ?>
Output
The above program displays the following output
key: 0 value: 1 key: 1 value: 4 key: 2 value: 9 key: 3 value: 16 key: 4 value: 25
The above is the detailed content of PHP generator class. For more information, please follow other related articles on the PHP Chinese website!

PHP报错:无法重复声明类,解决方法!对开发者而言,遇到问题是常有的事情。而在PHP开发中,经常会遇到一个常见的错误:无法重复声明类。这个问题看似简单,但如果不及时解决,会导致代码无法正确执行。本文将介绍这个问题的原因,并提供解决方法,以供参考。当我们在PHP代码中定义一个类时,如果在同一个文件或多个文件中多次定义同一个类,就会出现无法重复声明类的错误。这是

PHP中的命名规范:如何使用驼峰命名法命名类、方法和变量在PHP编程中,良好的命名规范是一种重要的编码实践。它可以提高代码的可读性和可维护性,并且使团队合作更加顺畅。在本文中,我们将探讨一个常见的命名规范:驼峰命名法,并提供一些示例来说明如何在PHP中使用它来命名类、方法和变量。一、什么是驼峰命名法?驼峰命名法是一种常用的命名约定,其中每个单词的首字母大写,

PHP中的封装技术及应用封装是面向对象编程中的一个重要概念,它指的是将数据和对数据的操作封装在一起,以便提供对外部程序的统一访问接口。在PHP中,封装可以通过访问控制修饰符和类的定义来实现。本文将介绍PHP中的封装技术及其应用场景,并提供一些具体的代码示例。一、封装的访问控制修饰符在PHP中,封装主要通过访问控制修饰符来实现。PHP提供了三个访问控制修饰符,

在Java开发过程中,有时候会遇到一个错误:java.lang.ClassNotFoundException。它表示在Java虚拟机(JVM)中找不到所需的类文件。这个错误会导致程序不能正常运行,如果不及时解决,会延误开发进度。本文将介绍Java中找不到类的原因和解决方法。一、原因1.类的路径错误在Java中,包路径和类路径很重要。如果类路径设置错误或者类文

PHP8中如何使用Attributes为类添加自定义注解?自定义注解是一种在类或方法上添加元数据的方式,它可以帮助我们在运行时获取和处理特定的类或方法上的附加信息。在PHP8中,引入了Attributes的概念,它使我们可以轻松地为类添加自定义注解。本文将介绍如何在PHP8中使用Attributes来实现类的自定义注解,并提供具体的代码示例。在PHP8中,自

什么是面向对象编程?面向对象编程(OOP)是一种编程范式,它将现实世界中的实体抽象为类,并使用对象来表示这些实体。类定义了对象的属性和行为,而对象则实例化了类。OOP的主要优点在于它可以使代码更易于理解、维护和重用。OOP的基本概念OOP的主要概念包括类、对象、属性和方法。类是对象的蓝图,它定义了对象的属性和行为。对象是类的实例,它具有类的所有属性和行为。属性是对象的特征,它可以存储数据。方法是对象的函数,它可以对对象的数据进行操作。OOP的优点OOP的主要优点包括:可重用性:OOP可以使代码更

PHP代码封装技巧:如何使用类和对象封装可重复使用的代码块摘要:在开发中,经常遇到需要重复使用的代码块。为了提高代码的可维护性和可重用性,我们可以使用类和对象的封装技巧来对这些代码块进行封装。本文将介绍如何使用类和对象封装可重复使用的代码块,并提供几个具体的代码示例。使用类和对象的封装优势使用类和对象的封装有以下几个优势:1.1提高代码的可维护性通过将重复

介绍匿名函数(也称为lambda)返回Closure类的对象。这个类有一些额外的方法,可以进一步控制匿名函数。语法Closure{ /*Methods*/ private__construct(void) publicstaticbind(Closure$closure,object$newthis[,mixed$newscope="static"


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
