CodeIgniter Forums
Issue using RecursiveIteratorIterator in a controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Issue using RecursiveIteratorIterator in a controller (/showthread.php?tid=76559)



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!