PHP实例化子类

erf*_*ing 4 php class instantiation parent

我想在这里成为一个面向对象的编码器,所以我给自己一些简单的任务.
我构建了一个显示给定目录中所有图像的类.这很好,所以我将该类分成两个类,一个用于读取目录中的文件名并将它们传递给一个数组,另一个用于解析该数组并显示图片.子类中的方法与父类中的方法完全相同(当然除了parent :: for this->).

现在看来,当我实例化子类并调用它的方法时,根本没有任何事情发生.

类别:

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/>';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

实例:

include("class.picFind.php");
$Myclass = new DisplayPics();
$Myclass->displayPics('./images/');
Run Code Online (Sandbox Code Playgroud)

Lev*_*son 5

说实话:你的整个设计都是错的.

  1. DisplayPics不应该继承Picfind. 老实说,要么有Picfind显示方法,要么从中DisplayPics获取输出Picfind.想一想,以下是否有意义:"DisplayPics是一个PicFind."?如果没有,那可能是错的.
  2. 类通常不是动词. 一个更好的名称是Pictures,用 finddisplay方法.在您的情况下,您在目录中找到了一些东西,这导致了下一点:
  3. 您应该使用PHP DirectoryIterator类.这样,您可以使用找到的文件执行任何操作.您将获得有关该文件的所有信息,并且它与PHP很好地集成.
  4. 你需要分离关注点.这就是哈克尔的建议.减少依赖关系和解耦事物通常很有帮助.

/**
 * 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'
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用:

$iterator = new PictureFinder('img/');
foreach($iterator as $file) {
    //do whatever you want with the picture here.
    echo $file->getPathname()."\n";
}    
Run Code Online (Sandbox Code Playgroud)

请注意,您可以使用ExtensionFinder上面定义的类来查找任何扩展名的文件.这可能比简单地找到图像更有用,但我PictureFinder为这个特定的用例定义了一个类.