PHP instance of a child class

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/'); 
+4
source share
4 answers

You wrote that you want to learn object-oriented programming. How about the following:

 class PicFinder { /** * @return array */ public function inDirectory($directory) { return // array of files } } class PicPresentation { public function present(array $pictures) { // your presentation code } } $path = '/your/path'; $datasource = new PicFinder(); $presentation = new PicPresentation(); $pictures = $datasource->inDirectory($path); $presentation->present($pictures); 

Keep things separate and loosely coupled. One object must be responsible for one thing, for example. one object to get a list of images from the catalog and another for presentation. Good luck

+2
source

Honestly: the whole design is wrong.

  • DisplayPics should not inherit from Picfind . If you have a Picfind display method, or DisplayPics output the result from Picfind . Think about whether the following makes sense: "DisplayPics is PicFind"? If not, this may be wrong.
  • Classes are usually not verbs. The best name would be Pictures , with the find and display methods. In your case, you find something in the directory, which leads to the following item:
  • You must use the PHP DirectoryIterator class. . That way, you can do whatever you want with the files you find. You will have all the file information available to you, and it integrates perfectly with PHP.
  • You need a separation of concerns. This is what hakre offers. Dependency reduction and decoupling are usually useful.

 /** * 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 (isset($this->extensions) && count($this->extensions)>0) { //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"; } 

Please note that you can use the ExtensionFinder I class above to search for ANY extension files. This could potentially be more useful than just searching for images, but I defined a PictureFinder class for it for this particular use case.

+5
source

$ Myclass-> displayPics ('./images/'); calls the constructor and nothing happens. You also have a typo in the name of your function.

0
source

Instead, I suggest this design:

 class PicFinder { public function findPics($dir){ ... } } class PicDisplayer { protected $picFinder; public function __construct() { // Default pic finder $this->setPicFinder(new PicFinder()); } public function diplayPics($dir) { echo 'displayPics method called'; foreach($this->getPicFinder()->findPics($dir) as $key => $val) { echo '<img src="' . $dir . $val . '" img><br/>'; } } protected function setPicFinder(PicFinder $picFinder) { $this->picFinder = $picFinder; } protected function getPicFinder() { return $this->picFinder; } } 

This way you use PicDisplayer, and no matter how it finds the photos. But if necessary, you can change the "PicFinder" by extending the PicFinder class and performing certain behavior.

0
source

Source: https://habr.com/ru/post/1381778/


All Articles