【发布时间】:2016-02-25 19:52:58
【问题描述】:
我已经尝试调试这段代码一段时间了,看起来build 方法没有被调用。我在其中放入了 echo's、var_dumps 和所有其他类型的符号,但从未得到任何东西。
完整的php代码
class Auto_slideshow {
private $_img_dir;
//constructor sets directory containing the images
function __construct($dir) {
//Directory source from webroot
$_img_dir = $dir;
}
//Iterates through directory and constructs HTML img list
public function build() {
//use SPL for directory iteration
$iterator = new DirectoryIterator($_img_dir);
$html = '';
$i = 1;
//Iterate through directory
foreach ($iterator as $file) {
//set a variable to add class of "show" on first element
$showClass = $i === 1 ? ' class="show"' : null;
//exclude '.' and '..' files
if(!$iterator->isDot()) {
//set src attribute for img tag
$src = $_img_dir . $iterator->getFilename();
//Grab sizes of images
$size = getimagesize($_img_dir . $iterator->getFilename());
var_dump($src);
//create img tags
$html .= '<img src="' . $src . '" ' . $size[3] . $displayNone . ' />' . "\r\n";
$i++;
}
}
return $html;
}
}
html调用
<center>
<div class="imagecontainer" id="auto-slideshow">
<?php
$show = new Auto_slideshow("../CMSC/images/master/slidestock/");
$show->build();
?>
</div>
</center>
我也尝试了print $show->build();,正如the tutorial 显示的那样,它也没有用。
更新
我将$_img_dir 更改为$this->$_img_dir' and called the method byecho show->build();` 仍然没有调用该方法。
这是一个方法甚至没有运行的问题,甚至还没有找到图像。
更新 2
如果我删除了 html 中的整个 php 代码,页面的其余部分就会正常加载。就像现在一样,html 仅加载到包含 php 的 div,然后停止它之后的所有内容。
【问题讨论】:
-
$_img_dir没有分配给任何东西。如果你想给类属性赋值,你必须使用$this->_img_dir。 -
我认为当您结合 @jeroen 和 Kevins 的答案时,如果您的 img_dir 存在,您将有一个可行的解决方案;)