我想成为一个面向对象的程序员,所以我给自己一些简单的任务。
我构建了一个类来显示给定目录中的所有图像。这工作得很好,所以我将这个类分成两个类,一个读取目录中的文件名并将其传递到数组中,另一个解析该数组并显示图片。子类中的方法与父类中的方法完全相同(当然,除了将parent::替换为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/>';
}
}
}
实例化:
include("class.picFind.php");
$Myclass = new DisplayPics();
$Myclass->displayPics('./images/');
说实话,你的整个设计都是错误的。
-
DisplayPics
不应该继承Picfind
, 老实说,要么让Picfind
有一个显示方法,要么让DisplayPics
从Picfind
获取输出。想想看,下面这句话有意义吗:"DisplayPics是一个PicFind"?如果没有,可能是错误的。 -
类通常不是动词。更好的名字应该是
Pictures
,find
和display
方法。在您的示例中,您正在目录中查找某些内容,这将导致下一点: -
您应该使用PHP的
DirectoryIterator
类。这样,您就可以对找到的文件做任何您想做的事情。您将获得关于该文件的所有信息,并且它与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'
);
}
}
如何使用:
$iterator = new PictureFinder('img/');
foreach($iterator as $file) {
//do whatever you want with the picture here.
echo $file->getPathname()."n";
}
注意,您可以使用我上面定义的ExtensionFinder
类来查找任何扩展名的文件。这可能比简单地查找图像更有用,但是我为您定义了一个PictureFinder
类,用于特定的用例。
你写过你想学习面向对象编程。
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);
保持事物分离和松散耦合。一个对象应该负责一件事,例如一个对象从目录中获取图片列表,另一个对象负责演示。好运!
Myclass -> displayPics('。/图片/');正在调用构造函数,但什么也没发生。你的函数名也有一个错别字
我建议这样设计:
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;
}
}
这样你只使用picdisplay而不关心它如何找到图片。但是,如果需要,您仍然可以通过扩展PicFinder类并实现特定的行为来更改"PicFinder"。