6

I want to scan directories and subdirectories, make list of xml files, take content from xml files and display it. This functions work correctly without OOP. I try to create a class. I call function scandir_through from function main. I haven't errors, result too.

class data {
    var $dir = 'D:\wamp4\www\begin';

    public function scandir_through($dir)
     {
         $items = glob($dir . '/*');
         for ($i = 0; $i < count($items); $i++) {
             if (is_dir($items[$i])) {
                 $add = glob($items[$i] . '/*');
                 $items = array_merge($items, $add);
             }
         }
        return $items;
     }

    public function main()
    {
        $scan_tree = $this->scandir_through($dir);
        echo "<ul id='booklist'>"."</n>";
        foreach ($scan_tree as $key=>$file){
            $url = $file;
            $xml = simplexml_load_file($url);  
               $book_count = count($xml->book); 
               for($i = 0; $i < $book_count; $i++) { 
                   $book = $xml->book[$i]; 
                   $title=$xml->book[$i]->title;
                   $author=$xml->book[$i]->author;
                   //echo '</br>';
                   //echo $file. " &nbsp   ";
                   //echo $title. " &nbsp   ";
                   //echo $author;
                   echo "<li><div class='file'>".$file."</div>
                   <div class='title'>".$title."</div>
                   <div class='author'>".$author."</div></li></n>";
               }     
         }  
          echo "</ul>";
    }
}
$d = new data();
$d->main();
?>
4
  • 1
    Please remember that PHP is a multi-paradigm language. You don't need to use OOP everywhere. Commented Aug 8, 2013 at 17:56
  • No, my mystake, incorrect copy. Commented Aug 8, 2013 at 17:56
  • OOP is need for this task. Commented Aug 8, 2013 at 17:57
  • 1
    kindly provide the output of $items Commented Aug 8, 2013 at 17:59

3 Answers 3

20

The problem is because the $dir instance variable isn't what you're accessing within your main method. (It's looking for a $dir variable in the scope of that method, rather than at the class level.)

What you need to use is...

$scan_tree = $this->scandir_through($this->dir);

If you turn on E_NOTICE warnings, you'll see that it'll have been throwing an error.

Sign up to request clarification or add additional context in comments.

1 Comment

I was tryed this, but I have a blank page, no result.
3

I think that $dir should be an argument for main. Just think if you had nothing else in the class, where would main get $dir from?

I would change:

public function main()

to

public function main($dir)

and when you call main using $d, include the dir so change that to:

$d->main($dir);

1 Comment

@vili if $d->main($dir) doesn't work try it without since you already have the $dir in the class.
1
$items=data::scandir_through($dir);

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.