Philll_t
Philll_t

Reputation: 4437

PHP Directory Iterator cannot be used as an array

A strange thing is happening here and I'm not entirely sure why this is happening.

I created a class which automatically echoes out the include tags for css and js. It uses the direcotryiterator in php. Here are the two methods:

   function _setup_js() {
            // Check for js folder in the $_active url
            if (file_exists(SITE_ROOT . "/view/" . $this->view . "/js")) {
                // Loop through them and echo js includsion
                $js_dir = new DirectoryIterator(SITE_ROOT . "/view/" . $this->view . "/js");
                foreach ($js_dir as $fileinfo) {
                    if ($fileinfo->getFilename() !== "." AND $fileinfo->getFilename() !== "..") {
                        echo "<script type='text/javascript' src='" . SITE_URL . "view/" . $this->view . "/js/" . $fileinfo->getFilename() . "'></script>";
                    }
                }
            }

        }

        function _setup_css() {
            // Check for js folder in the $_active url
            if (file_exists(SITE_ROOT . "/view/" . $this->view . "/css")) {
                // Loop through them and echo js includsion
                $css_dir = new DirectoryIterator(SITE_ROOT . "/view/" . $this->view . "/css");
// The following line is line 30                
foreach ($css_dir as $fileinfo) {
                    if ($fileinfo->getFilename() !== "." AND $fileinfo->getFilename() !== ".." AND $fileinfo['extension'] === "css") {
                     echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . SITE_URL . "view/" . $this->view . "/css/" . $fileinfo->getFilename() . "\">";
                    }
                }
            }
        }

The _setup_js() method works with flying colors, but when I call the _setup_css() method it spits out this error:

[error] 6545#0: *105 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Cannot use object of type DirectoryIterator as array in /Users/usr/Projects/com.project.sandbox/lib/lib_frontendauto.php on line 30

As you can see, the methods are practically identical with the exception of the file type accommodations.

Upvotes: 0

Views: 296

Answers (1)

Albert Kozłowski
Albert Kozłowski

Reputation: 476

Try changing this:

AND $fileinfo['extension'] === "css") {

To this:

AND $fileinfo->getExtension () === "css") {

Upvotes: 2

Related Questions