PHP实例化子类
我想成为一名面向对象的编码员,所以我给自己布置了一些简单的任务。
我构建了一个显示给定目录中所有图像的类。这工作得很好,所以我将该类分成两个类,一个类读取目录中的文件名并将它们传递到一个数组中,另一个类解析该数组并显示图片。子类中的方法与父类中的方法完全相同(当然除了用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/');
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/');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
说实话:你的整个设计都是错误的。
DisplayPics
不应该继承自Picfind
。老实说,要么有Picfind
有一个显示方法,或者让DisplayPics
获取Picfind
的输出。想一想,以下内容是否有意义:“DisplayPics 是一个 PicFind。”?如果不是,那可能是错误的。Pictures
,其中包含find
和display
方法。在你的例子中,你在目录中找到了一些东西,这导致了下一点:DirectoryIterator
类。 这样,您就可以对找到的文件执行任何您想要的操作。您将获得有关该文件的所有信息,并且它与 PHP 完美集成。如何使用:
请注意,您可以使用我上面定义的
ExtensionFinder
类来查找任何扩展名的文件。这可能比简单地查找图像更有用,但我为该特定用例定义了一个PictureFinder
类。To be honest: your whole design is wrong.
DisplayPics
shouldn't inherit fromPicfind
. Honestly, either havePicfind
have a display method, or haveDisplayPics
take the output fromPicfind
. Think, does the following make sense: "DisplayPics is a PicFind."? If not, it's probably wrong.Pictures
, withfind
anddisplay
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.How to use:
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 aPictureFinder
class for you for that specific use-case.您写道您想学习面向对象编程。那么以下呢:
保持事物分离和松散耦合。一个对象应该负责一件事,例如,一个对象负责从目录中获取图片列表,另一个对象负责演示。祝你好运!
You wrote that you want to learn object oriented programming. What about the following:
Keep things separated and loosely coupled. One object should be responsible for one thing, e.g. one object to obtain the list of pictures from a directory and another one for the presentation. Good luck!
$Myclass->displayPics('./images/');
正在调用构造函数,但什么也没发生。
您的函数名称也有拼写错误。
$Myclass->displayPics('./images/');
is calling the constructor and nothing is happening.
You have a typo in your function name aswell.
我建议改为这样的设计:
这样你只使用 PicDisplayer 并且不关心它如何找到图片。但如果需要,您仍然可以通过扩展 PicFinder 类并实现特定行为来更改“PicFinder”。
I'd suggest that design instead:
That way you only use PicDisplayer and don't care how it finds the pics. But you can still change the "PicFinder" if needed, by extending the PicFinder class and implementing a specific behavior.