note 121273 added to class.traversable
| From: | douglas at reith dot com dot au | Date: | Mon, 26 Jun 2017 08:09:44 +0000 |
| Subject: | note 121273 added to class.traversable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209150@lists.php.net to get a copy of this message | ||
The PHP7 iterable pseudo type will match both Traversable and array. Great for return type-hinting
so that you do not have to expose your Domain to Infrastructure code, e.g. instead of a Repository
returning a Cursor, it can return hint 'iterable':
<?php
UserRepository::findUsers(): iterable
?>
Link: http://php.net/manual/en/migration71.new-features.php#migration71.new-features.iterable-pseudo-type
Also, instead of:
<?php
if( !is_array( $items ) && !$items instanceof Traversable )
//Throw exception here
?>
You can now do with the is_iterable() method:
<?php
if ( !is_iterable( $items ))
//Throw exception here
?>
Link: http://php.net/manual/en/function.is-iterable.php
----
Server IP: 72.52.91.14
Probable Submitter: 150.101.223.223
----
Manual Page -- http://php.net/manual/en/class.traversable.php
Edit -- https://master.php.net/note/edit/121273
Del: integrated -- https://master.php.net/note/delete/121273/integrated
Del: useless -- https://master.php.net/note/delete/121273/useless
Del: bad code -- https://master.php.net/note/delete/121273/bad+code
Del: spam -- https://master.php.net/note/delete/121273/spam
Del: non-english -- https://master.php.net/note/delete/121273/non-english
Del: in docs -- https://master.php.net/note/delete/121273/in+docs
Del: other reasons-- https://master.php.net/note/delete/121273
Reject -- https://master.php.net/note/reject/121273
Search -- https://master.php.net/manage/user-notes.php