CodeIgniter Forums
Issue using RecursiveIteratorIterator in a controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Issue using RecursiveIteratorIterator in a controller (/thread-76559.html)



Issue using RecursiveIteratorIterator in a controller - kilometrocero - 05-25-2020

I am attempting to call php's RecursiveIteratorIterator through a function in my controller class and I get the debugger message, "Class 'App\Controllers\RecursiveIteratorIterator' not found." My controller operates fine without this, although I would like to have the option to use it. Here is my code. Why does it think I am trying to call a controller and how can I remedy the error? Thank you in advance for your consideration.
PHP Code:
$newDataIterator = new RecursiveIteratorIterator
   new RecursiveArrayIterator(json_decode($dataArrayTRUE)),
   RecursiveIteratorIterator::SELF_FIRST
); 



RE: Issue using RecursiveIteratorIterator in a controller - kilishan - 05-25-2020

It's a namespace issue. Within your controller, you're in the namespace App\Controllers. The RecursiveIteratorIterator is NOT in that namespace, so you must prefix it with a backslash, or add it as a use statement at the top of the file. That allows PHP to know to look for it in the root namespace, not in the current namespace (App\Controllers).

PHP Code:
$newDataIterator = new \RecursiveIteratorIterator
   new \
RecursiveArrayIterator(json_decode($dataArrayTRUE)),
   \
RecursiveIteratorIterator::SELF_FIRST
); 

or

PHP Code:
<?php namespace App\Controllers;

use 
RecursiveIteratorIterator;
use 
RecursiveArrayIterator;

  public function 
index() {
    
$newDataIterator = new RecursiveIteratorIterator
        new 
RecursiveArrayIterator(json_decode($dataArrayTRUE)),
        
RecursiveIteratorIterator::SELF_FIRST
    
); 
  }




RE: Issue using RecursiveIteratorIterator in a controller - kilometrocero - 05-25-2020

Wonderful. I am happy I asked. Thank you!