Jump to content

OOP Not working quite right


interfacer

Recommended Posts

Hi All :)  I recently just purchased a book on OOP in PHP 5 and have been working through it.  I ran into a wall when I've been trying to use an imageOnly() method to sort out any non image files before they are displayed. 

Here is the constructor:
[code]  public function __construct($directory, $replacechar = "_") {
    $this->directory = $directory;
    $this->replacechar=$replacechar;
    $d="";
    if(is_dir($directory)){
    $d = opendir($directory) or die("Couldn't open directory.");
    while(false !== ($f=readdir($d))){
      if(is_file("$directory/$f")){
      $this->filearray[] = $f;
      }
    }
    closedir($d);
    }else{
    //error
    die("Must pass in a directory.");
    }
  }[/code]

Here's the imageOnly() method:
[code]  public function imagesOnly(){
    $extension = "";
    $types = array("jpg", "jpeg", "gif", "png");
    foreach($this->filearray as $key => $value){
    $extension = substr($key,(strpos($key, ".") + 1));
    $extension = strtolower($extension);
    if(!in_array($extension, $types)){
      unset($this->filearray[$key]);
    }
    }
  }[/code]

and finally, here's what I'm trying to output it with:
[code]<?
require 'DirectoryItems.php';
$directory = "test";
$di = new DirectoryItems($directory);
$di->naturalCaseInsensitiveOrder();
$di->imagesOnly() or die("imagesOnly() isn't working");
$filearray=$di->getFileArray();

echo "<div style=\"text-align: center;\">";
foreach($filearray as $key => $value) {
echo "<img src=\"test/$value\"><br />\n";
}
echo "</div><br />";
?>[/code]

My class file is named as DirectoryItems.php. if I take out the imagesOnly line, it works fine as long as there are no non-image files in my directory.  Any help in assessing why that method is causing it to appear as "imagesOnly() isn't working" would be wonderful.  Thank you all for your help, I sincerely appreciate it.
Link to comment
https://forums.phpfreaks.com/topic/23319-oop-not-working-quite-right/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.