1

I'm using laravel 5.6, and in my controller I have

use RecursiveIteratorIterator; 

and the error I'm getting is

Class 'App\Http\Controllers\RecursiveArrayIterator' not found 

So I'm being a little simplistic about it. Obviously RecursiveIteratorIterator is not a laravel function, but rather a core php function. But after reading this post https://laraveldaily.com/how-to-use-external-classes-and-php-files-in-laravel-controller/ I'm still no clearer on where to find it, or how to reference it properly.

I suppose I was expecting that all "native" php functions would just be available?

Help?

3
  • 1
    Its core code so try use \RecursiveIteratorIterator; Commented Aug 1, 2018 at 23:42
  • Can you show us the line that's generating the Class not found error? Commented Aug 2, 2018 at 0:22
  • It was $jsonIterator = new RecursiveIteratorIterator( new RecursiveArrayIterator(json_decode($request, TRUE)), RecursiveIteratorIterator::SELF_FIRST); Commented Aug 2, 2018 at 0:41

2 Answers 2

3

You want to put a backslash in front (No need to put use ... at the top), to specify a root/global class:

\RecursiveIteratorIterator; 

For more information, refer to the PHP documentation here:

Example #3 Accessing internal classes in namespaces

<?php
namespace foo;
$a = new \stdClass;

function test(\ArrayObject $typehintexample = null) {}

$a = \DirectoryIterator::CURRENT_AS_FILEINFO;

// extending an internal or global class
class MyException extends \Exception {}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, right. Thanks. So it seems the use \RecursiveIteratorIterator; is uneccessary (at the top), but in the body, prefix the function with a "\" does the trick.
1

Just to be mention, it looks like you are only importing

use \RecursiveIteratorIterator;

and also calling RecursiveArrayIterator somewhere in the code. try importing both.

   use \RecursiveIteratorIterator;
   use \RecursiveArrayIterator;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.