简体   繁体   中英

Using RecursiveIteratorIterator with namespaces?

My problem is that when I'm using namespaces, I can't use the RecursiveIteratorIterator Class of the Standard PHP Library. The following code :

<?php namespace mynamespace;

    $arr = Array("abc", Array("def", "ghi", Array("jkl")));
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
    $result = iterator_to_array($it, false);

?>

Returns:

PHP Fatal error: Uncaught Error: Class 'mynamespace\\RecursiveIteratorIterator' not found in ...

You need to reference the global namespace with \\ :

$it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr));

Or you can import them into the current namespace with use :

namespace mynamespace;
use RecursiveIteratorIterator;
use RecursiveArrayIterator;

See Using namespaces: Basics for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM