I'm trying to become an object-oriented coder here, so I'm giving myself some simple tasks.
I built a class that displays all the images in a given directory. That worked fine, so I separated that class into two classes, one to read the filenames in the directory and pass them into an array, and one to parse that array and display the pictures. The method in the child class is exactly the same as it was when it was in the parent class (except of course substituting parent:: for this->).
Now it seems when I instantiate the child class and call its method, nothing happens at all.
Classes:
class Picfind
{
public function findPics($dir){
$files = array();
$i=0;
$handle = opendir($dir);
while (false !== ($file = readdir($handle))){
$extension = strtolower(substr(strrchr($file, '.'), 1));
if($extension == 'jpg' || $extension == 'gif' || $extension == 'png'){
// now use $file as you like
$i++;
$files['file' . $i] = $file;
}
}
return $files;
}
}
class DisplayPics extends Picfind
{
function diplayPics($dir)
{
echo 'displayPics method called';
foreach(parent::findPics($dir) as $key => $val) {
echo '<img src="' . $dir . $val . '" img><br/>';
}
}
}
Instantiation:
include("class.picFind.php");
$Myclass = new DisplayPics();
$Myclass->displayPics('./images/');
To be honest: your whole design is wrong.
DisplayPics
shouldn't inherit from Picfind
. Honestly, either have Picfind
have a display method, or have DisplayPics
take the output from Picfind
. Think, does the following make sense: "DisplayPics is a PicFind."? If not, it's probably wrong.Pictures
, with find
and display
methods. In your case, you are finding something in a directory, which leads to the next point:DirectoryIterator
class. This way, you can do whatever you'd like with the files you find. You'll have all the information about the file available to you, and it's integrated nicely with PHP./**
* ExtensionFinder will find all the files in a directory that have the given
* extensions.
*/
class ExtensionFinder extends DirectoryIterator {
protected $extensions = array();
public function __contruct($directory) {
parent::__construct($directory);
}
/**
* Sets the extensions for the iterator.
* @param array $extensions The extensions you want to get (without the dot).
*/
public function extensions(array $extensions) {
$this->extensions = $extensions;
}
/**
* Determines if this resource is valid. If you return false from this
* function, the iterator will stop.
* @return boolean Returns true if the value is a file with proper extension.
*/
public function valid() {
if (parent::valid()) {
$current = parent::current();
if ($current->isFile()) {
// if the extensions array is empty or null, we simply accept it.
if (empty($this->extensions)) {
//otherwise filter it
if (in_array($current->getExtension(), $this->extensions)) {
return true;
} else {
parent::next();
return $this->valid();
}
} else {
return true;
}
} else {
parent::next();
return $this->valid();
}
} else {
return false;
}
}
}
class PictureFinder extends ExtensionFinder {
public function __construct($directory) {
parent::__construct($directory);
$this->extensions = array (
'jpg',
'gif',
'png'
);
}
}
How to use:
$iterator = new PictureFinder('img/');
foreach($iterator as $file) {
//do whatever you want with the picture here.
echo $file->getPathname()."\n";
}
Note that you could use the ExtensionFinder
class I defined above to find files of ANY extension. That could potentially be more useful than simply finding images, but I defined a PictureFinder
class for you for that specific use-case.