Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / PHP

Traversable

Značkovacie rozhranie objektov s iteráciou.

Verzia: PHP 8.4; repository runtime, production runtime not attested

Syntax

function collect(Traversable $items): array

Značkovacie rozhranie objektov s iteráciou. Od PHP 8 môže abstraktná trieda implementovať značku priamo. Konkrétna trieda musí zabezpečiť Iterator alebo IteratorAggregate. Polia vyhovejú iterable, ale nie sú objektmi Traversable. Na príjem oboch tvarov použite iterable.

function collect(Traversable $items): array {return iterator_to_array($items,false);}
$cursor=new ArrayIterator([2,4]); echo json_encode([collect($cursor),$cursor instanceof Traversable,[] instanceof Traversable,is_iterable([])]);
abstract class StreamBase implements Traversable {} class One extends StreamBase implements IteratorAggregate {public function getIterator(): Traversable {yield 8;}}
echo PHP_EOL,json_encode(collect(new One()));
[[2,4],true,false,true]
[8]

Príklady

<?php
function collect(Traversable $items): array {return iterator_to_array($items,false);}
$cursor=new ArrayIterator([2,4]); echo json_encode([collect($cursor),$cursor instanceof Traversable,[] instanceof Traversable,is_iterable([])]);
abstract class StreamBase implements Traversable {} class One extends StreamBase implements IteratorAggregate {public function getIterator(): Traversable {yield 8;}}
echo PHP_EOL,json_encode(collect(new One()));

Výsledok

[[2,4],true,false,true]
[8]

Zdroje