No ratings yet.

what are iterators in php

Iterators provide a way for PHP to loop through an arbitrary data set using a foreach loop. Predefined iterators exist in the SPL to loop through arrays, directories, and XML. Another common use of iterators, and one that you will be implementing in this chapter, is to loop through a MySQL result set. You will also be using several other interfaces from the SPL to create array-like
functionality for your object.
A class that retrieves the data A class that extends one of the SPL classes to provide pagination functionally
A Book class that stores, modifies, and accesses the book information
The Iterator Interface The fi rst interface to implement is the aptly named Iterator interface. The Iterator interface provides
a set of five methods that must be implemented in order for iteration to work with built-in PHP constructs. The Iterator design pattern itself is well established and is not exclusive to PHP. The SPL interface is simply a standardization of the methods so that they can be reliably implemented across all PHP applications.
All iterators, in any language, must be able to perform four actions:

  1. Rewind the iterator to the first element.Advance to the next element
  2. Check to see if the iterator has reached the last element.
  3. Get the current item pointed to by the iterator’s internal pointer

The PHP interface defines these four methods as well as a fi fth (bolded in the code) to replicate PHP’s treatment of arrays as hash tables. Here is the basic layout of a class that implements the interface:
<?php
class BookList implements Iterator {
public function current() { .. }
public function key() { .. }
public function next() { .. }
public function rewind() { .. }
public function valid() { .. }
public function next() { .. }
public function rewind() { .. }

}
?>

 

LAMBDA FUNCTIONS AND CLOSURES:

A lambda function (also commonly referred to as an anonymous function) is, as its alternative name implies, a function that does not require a name when it is defi ned. Instead, lambda functions are assigned to a variable that can then be used to call that function. They are used as callback functions and in other situations where it is unnecessary or undesirable to defi ne a function in the global scope.

 

Lambda-Style Functions

Lambda-style functions are something that has been part of PHP since version 4.0.1 via the create_ function() call. They are called lambda-style functions because they are not truly anonymous. The function will fi nd a unique name that is not currently being used as an identifi er elsewhere in the script and then use that as the name for the newly created function. Usually the generated function will be called lambda_N where N is an integer starting at one and lambda_N is not yet defi ned.

 

Understanding Closures in php

Closures are a concept that should be very familiar to JavaScript programmers as well as programmers for many other modern languages. A closure is a function that wraps (or closes around) the current scope. It has the implication that the scope in which the function is defi ned will remain accessible for at least as long as the closure itself. To create a closure, fi rst, create a new lambda
function in PHP:
<?php
$y = function($number) {
return $number * 20;
};
echo $x(8); // Output: 160
?>
How the function isn’t given a name and the result is assigned to a variable. Lambda functions created this way are returned as objects of type closure. This can be a source of confusion. Although closure is created transparently in this situation it does not take advantage of any of the properties of a closure.

Lambda functions take advantage of new functionality in PHP 5.3: the ability to call an object as if it were a function. It is possible to emulate the functionality of the closure class by using a new magic method called __invoke(). The __invoke() method will be called whenever the class is called as a function.
As closures are stored in variables they can be returned from functions and executed outside the scope of the function in which they are defi ned. The similarities between PHP and many languages with closure end here. Variables are not accessible inside functions unless they are declared as global. In much the same way, variables from the child scope are not accessible from within the closure unless explicitly stated using the use keyword.
One of the benefits of closures is that variables inside of the closure are not necessarily bound to copies of the variables in the parent scope, but can be bound to the actual variables themselves. In a true closure such as JavaScript the latter is usually the case. In PHP, variables are copied into the closure by value.

Any function in PHP that takes a parameter of type Callback can be one of four types:

  1. A string with the name of the function to call.
  2. The create_function() example uses this method.
  3. An array of two elements with the first element as an object and the second element as the name of a method in the object.
  4. A string representing a static method (example: “hello::world”). A certain type of object (more on this one next)

spl in php:

  1. ArrayAccess is an interface that allows the class to be accessed via standard array notation. It specifi es the methods needed to read, update, delete, and check the validity of data at specific keys.
  2. Countable allows any class that implements it to be enumerated via the PHP count() function, which bears the same name as the interface’s only method definition.
  3. Iterator is used to enable a data set to be easily traversed by standard foreach loops. It defines the methods next(), current(), rewind(), valid(), and key().
  4. SeekableIterator extends the basic Iterator interface and adds a definition for a seek() method. The seek() method should throw an OutOfBoundsException if the index being sought is not valid.
php oop tutorial

Please rate this