Welcome Guest, Not a member yet? Register   Sign In
Issue using RecursiveIteratorIterator in a controller
#1

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
); 
Reply
#2

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
    
); 
  }

Reply
#3

Wonderful. I am happy I asked. Thank you!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB