I am trying to become an object-oriented encoder, so I give myself some simple tasks.
I built a class that displays all the images in a given directory. This worked well, so I divided this class into two classes: one to read the names of files in a directory and pass them to an array, and the other to parse this array and display images. The method in the child class is exactly the same as in the parent class (except, of course, replacing parent :: for this →).
Now, 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/');
source share