downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Iterator> <Interfaces predefinidas
[edit] Last updated: Fri, 07 Jun 2013

view this page in

La interfaz Traversable

(PHP 5 >= 5.0.0)

Introducción

Interfaz para detectar si una clase puede ser recorrida mediante foreach.

Una interfaz abstracta base no puede ser implementada sola. En su lugar, debe ser implementada con IteratorAggregate o con Iterator.

Nota:

Las clases internas que implementan esta interfaz pueden ser usadas en una construcción foreach y no necesitan implementar IteratorAggregate o Iterator.

Nota:

Este es un motor interno de interfaz que no puede ser implementado en scripts de PHP. Se debe emplear en su lugar o IteratorAggregate, o bien Iterator. Cuando se implementa una interfaz que extiende a Traversable, asegúrese de enumerara IteratorAggregate o Iterator antes de su nombre en la cláusula de implementación.

Sinopsis de la Interfaz

Traversable {
}

Esta interfaz no tiene métodos; su único propósito es servir de interfaz base para todas las clases que se pueden recorrer.



add a note add a note User Contributed Notes Traversable - [1 notes]
up
11
kevinpeno at gmail dot com
2 years ago
While you cannot implement this interface, you can use it in your checks to determine if something is usable in for each. Here is what I use if I'm expecting something that must be iterable via foreach.

<?php
   
if( !is_array( $items ) && !$items instanceof Traversable )
       
//Throw exception here
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites