0

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.

2
  • I dont know but it sounds to me like, it has more than 1 css linked and since there is more than 1 css linked, it is just not parsing it as an array. Commented Nov 16, 2013 at 7:12
  • I've updated the question, please see the comment // the following line… Commented Nov 16, 2013 at 7:22

1 Answer 1

2

Try changing this:

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

To this:

AND $fileinfo->getExtension () === "css") {
Sign up to request clarification or add additional context in comments.

1 Comment

KABOOM! Thank you Albert!

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.