嗷嗷答 - 嗷嗷好的公务员考试网络答题平台

TYPECHO核心代码中的一处错误

作者:不烦恼 发布时间:May 8, 2011 分类:快乐学习

错误描述:当重复执行

  1. <?php while($this->next()): ?>
时,堆栈会额外增加一条数据
源代码:Var/Typecho/Widget.php

  1. /**
  2.   * 返回堆栈每一行的值
  3.   *
  4.   * @return array
  5.   */
  6. public function next()
  7. {
  8.  
  9. if ($this->stack) {
  10. $this->row = &$this->stack[key($this->stack)];
  11. next($this->stack);
  12. $this->sequence ++;
  13. }
  14.  
  15. if (!$this->row) {
  16. reset($this->stack);
  17. if ($this->stack) {
  18. $this->row = $this->stack[key($this->stack)];
  19. }
  20. $this->sequence = 0;
  21. return false;
  22. }
  23.  
  24. return $this->row;
  25. }

上面的代码可以简化如下:

  1. <?php
  2. $stack = array(1,2,3);
  3.  
  4. $row = &$stack[key($stack)];
  5.  
  6. next($stack);
  7. $row = &$stack[key($stack)];
  8.  
  9. next($stack);
  10. $row = &$stack[key($stack)];
  11.  
  12. next($stack);
  13. $row = &$stack[key($stack)];
  14. $row = $stack[0];
  15.  
  16. print_r($stack); // Array ( [0] => 1 [1] => 2 [2] => 3 [] => 1 )
  17.  
  18. ?>

解决方法,将“&”换成“@”

  1. /**
  2.   * 返回堆栈每一行的值
  3.   *
  4.   * @return array
  5.   */
  6. public function next()
  7. {
  8.  
  9. if ($this->stack) {
  10. $this->row = @$this->stack[key($this->stack)];
  11. next($this->stack);
  12. $this->sequence ++;
  13. }
  14.  
  15. if (!$this->row) {
  16. reset($this->stack);
  17. if ($this->stack) {
  18. $this->row = $this->stack[key($this->stack)];
  19. }
  20. $this->sequence = 0;
  21. return false;
  22. }
  23.  
  24. return $this->row;
  25. }

已有 16 条评论 »

  1. 解决方法,将“&”换成“@” 操作后还是无效。

    1. 不应该啊,我现在用的就是这个方法!
      抱歉,最近很忙真的是没时间上网!

      1. 嘻嘻,看着你上线激动。。。
        就是将它由&改成@对不
        row = @$this-

        php $this->widget('Widget_Archive@notice', 'type=category', 'mid=2', 'pageSize=6')->to($categoryPosts)->parse('{title}'); ?>

        好像这两个都可以用。
        可以显示不同,但是他们后面的都调用出来和他们一样
        php $this->widget('Widget_Archive@index', 'type=category', 'mid=2', 'pageSize=6')->to($categoryPosts)->parse('{title}'); ?>

    2. 兄弟 你神马时候回你blog 看看。

  2. CXM CXM

    是不是因为这个问题,导致在同一个页面内显示2次分类,会有部分ID分类重复了?

  3. 嘿嘿!学习咯!不错啊!

  4. 请问你在这个帖子中说的摘要实现方法,是怎么操作呢?
    http://forum.typecho.org/topic.php?id=1748

    能否普及一下?

    1. 不是很多人都需要,最近比较忙,暂不给回复了!

  5. 恭喜啊,公务员真强,第二名就更强了1

    1. 虽然第二,但分数很低,现在都不知道第一超她多少分,面试鸭梨很大!

      1. 公务员很有钱途的。。。

        1. 不仅有钱途,而且最主要的是能够有职业的保障
          你懂的:)

  6. 咦~~~以为进错地方了,排版变了?

    1. 呵,只是临时调整了一下

添加新评论 »