【问题标题】:Php method not being called未调用php方法
【发布时间】: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-&gt;build();,正如the tutorial 显示的那样,它也没有用。

更新

我将$_img_dir 更改为$this-&gt;$_img_dir' and called the method byecho show->build();` 仍然没有调用该方法。

这是一个方法甚至没有运行的问题,甚至还没有找到图像。

更新 2

如果我删除了 html 中的整个 php 代码,页面的其余部分就会正常加载。就像现在一样,html 仅加载到包含 php 的 div,然后停止它之后的所有内容。

【问题讨论】:

  • $_img_dir 没有分配给任何东西。如果你想给类属性赋值,你必须使用$this-&gt;_img_dir
  • 我认为当您结合 @jeroen 和 Kevins 的答案时,如果您的 img_dir 存在,您将有一个可行的解决方案;)

标签: php html methods


【解决方案1】:

修复并更新以下代码:

构造函数:

$this->_img_dir = $dir;

构建功能:

$iterator = new DirectoryIterator($this->_img_dir);

$src = $this->_img_dir 。 $iterator->getFilename();

$size = getimagesize($this->_img_dir . $iterator->getFilename());

你可以这样称呼它:

echo $show->build();

【讨论】:

  • 这会使页面崩溃。
  • 没关系,我遗漏了$。但它仍然没有加载任何图像。
【解决方案2】:

你返回了你想要显示的文本,但你没有显示它:

echo $show->build();

【讨论】:

    【解决方案3】:

    您是否也在循环外使用了var_dump()

    问题是您的变量将保持为 NULL:

      //constructor sets directory containing the images
      function __construct($dir) {
        //Directory source from webroot
    
        // This is a local variable that will be gone when the constructor finishes:
        $_img_dir = $dir;
      }
    

    你需要:

      //constructor sets directory containing the images
      function __construct($dir) {
        //Directory source from webroot
        $this->_img_dir = $dir;
         ^^^^^^ here
      }
    

    【讨论】:

    • 这会破坏页面并且什么都没有加载。
    • @CodyHarness 那么你可能在某个地方有错字。显然,您还需要在其他方法中使用相同的变量 $this-&gt;_img_dir
    • 我知道路径是正确的,我已经测试过了。我确实忘记了添加$this-&gt; 部分,但添加后它仍然中断。它仍然应该至少加载 php 调用上方的其余 html,就像以前一样。
    • @CodyHarness 我对文件迭代器不太熟悉,但您似乎在这里使用了错误的变量:$iterator-&gt;getFilename()。您可能需要$file-&gt;getFilename() 并且您现在遇到异常了吗?您应该启用错误处理/显示以查看确切的中断。
    • 没关系,我忘记了this 上的 $,但它仍然无法加载图像。我将尝试另一种迭代技术。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    相关资源
    最近更新 更多