php中的oop,在现有PHP中加入OOP

 2023-09-09 阅读 24 评论 0

摘要:DISCLAIMER: since question obviously has been already answered, this is a long form comment on provided code.编写类时,应尽量避免在构造函数中进行计算。这使得难以调试代码。我实际上会像这样构造代码:class ImageData {protected $count = 0;php面

DISCLAIMER: since question obviously has been already answered, this is a long form comment on provided code.

编写类时,应尽量避免在构造函数中进行计算。这使得难以调试代码。

我实际上会像这样构造代码:

class ImageData {

protected $count = 0;

php面向对象实例,protected $dir = null;

protected $dirArray = null;

public function __construct( $dir = 'panos/thumbs') {

$this->dir = $dir;

}

public function getCount(){

php的输出函数是?if ( is_array($this->dirArray) ){

$this->initialize();

}

return $this->count;

}

public function getArray(){

PHP定义活动变量的方法是,if ( is_array($this->dirArray) ){

$this->initialize();

}

return $this->dirArray;

}

protected function initialize(){

php代码的开始标记以下哪个不可以,$this->dirArray = $this->findImages();

sort($this->dirArray);

$this->count = count($dirArray);

}

protected function findImages( $extensions = array('jpg') ){

$files = array();

php oop。$dirCon = opendir($this->dir);

while ($name = readdir($dirCon)){

$current = strtolower(substr($foo, strrpos($foo, '.')+1));

if ( in_array( $current, $extensions ) ){

$files[] = $name;

}

以下不属于PHP7.2中的扩展的是。}

closedir($dirCon);

return $files

}

}首先,我建议使用protected而不是private,如果你现在不需要它们private。因为类的private成员不是“可见的”,所以在扩展类时。

另一件事是将对象的初始化移动到实际需要的位置。通过微小的修改,这个类可以让你重复搜索文件夹,甚至可以在不同的文件夹中搜索。

php new、如果您想学习如何在PHP的上下文中使用OOP,请首先阅读http://php.net/manual/en/language.oop5.php。你可能会发现PHP Object-Oriented Solutions书是一个很大的帮助。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/28537.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息