【问题标题】:PHP function called twice, funny warning messagePHP 函数调用了两次,有趣的警告信息
【发布时间】:2009-07-21 19:11:18
【问题描述】:

最终编辑:我已经将这个疯狂的问题总结并简化为一个新问题,所以我们可以关闭这个 Q 或任何必要的东西来标记它已解决。再次感谢!

新:

您能否告诉我您对此的看法,并可能看看您是否可以重新创建它:

目前,$post->post_content 变量包含:

"before <img src="/path/to/valid_img.gif" /> after"

这段代码放在主题header.php的顶部...

------ Code --------
    1: $str = $post->post_content;
    2: assert( isset( $str ) );
    3: assert( is_string( $str ) );
    4: echo $str;
    5: $str = 'before <img src="/path/to/nonexistant.gif" /> after';
    6: assert( isset( $str ) );
    7: echo $str;
--------------------

输出这个...

------ Output ------
before <img src="/path/to/valid_img.gif" /> after
before <img src="/path/to/nonexistant.gif" /> after
--------------------

有了这些警告...

--- PHP Warnings ---
PHP Warning:  assert() [<a href='function.assert'>function.assert</a>]: Assertion
failed in /path/to/header.php on line 2
PHP Warning:  assert() [<a href='function.assert'>function.assert</a>]: Assertion
failed in /path/to/header.php on line 3
--------------------

当我知道 100% 应该成功时,如果 assert() 失败两次,为什么第 4 行会正确地回显 $str?

损坏的图像 src 与设置或取消设置变量 $str 有什么关系? WordPress 错误/奇怪?

现在是疯狂的部分......

当第 5-7 行被注释掉时,从而消除了 nonexistant.gif,assert() 警告不会出现并且输出仍然正确地产生这个......

------ Output ------
before <img src="/path/to/valid_img.gif" /> after
--------------------

你有没有机会复制这个并告诉我你的想法?我是 PHP 新手,但我很确定这很疯狂。 :)

旧:

我对 PHP 比较陌生,我有这个代码:

$str = $post->post_content; // hi -- [hw] -- bye <img src="foobar.png" />
$str = core_wp( 'foo_shortcode', $str );
echo $str; // return $str; produces the same warning message
// If there is no echo or return, the warning message is not produced.
// The warning disappears when I statically set (as object) the initial $str to
// "hi -- [hw] -- bye <img src="foobar.png" />".

当我运行上面的代码时,它工作正常(foo-shortcode 正在做它的工作)。输出是:

hi -- Hello World -- bye <img src="foobar.png" />

但是我总是收到此警告消息,告诉我 foo-shortcode 似乎正在尝试第二次运行自己,除了第二次,$str 不存在。

还有一个问题...当 $str 包含指向不存在的 src 的 HTML img 标记时,我只会收到此警告消息。

PHP Warning:  Missing argument 1 for foo_shortcode() in ...

这里是 core-wp() 和 foo-shortcode():

function core_wp( $function, $a = NULL, $b = NULL )
{
    $wrap = array
    (
        'foo_shortcode'             => 'foo_shortcode',
    );
    $args = array();
    if ( isset( $a ) ) $args[] = $a;
    if ( isset( $b ) ) $args[] = $b;

    return call_user_func_array( $wrap[ $function ], $args );
}

function foo_shortcode( $content ) {
    return str_replace( '[hw]', 'Hello World', $content );
}

首先,为什么代码会正常工作,然后似乎同时尝试再次运行自己?

关于包含无效 img 的 $str,我怀疑这与 WordPress 生成 $post->post_content 的方式有关。

编辑 1(7 月 22 日,星期三,上​​午 8:55)

我按照您的指示添加了 error-log(),如下所示:

error_log( 'done1' );
$str = $post->post_content;
error_log( 'done2' );
$str = core_wp( 'foo_shortcode', $str );
error_log( 'done3' );

...它在我的错误日志中产生了这个:

[22-Jul-2009 08:52:49] done1
[22-Jul-2009 08:52:49] done2
[22-Jul-2009 08:52:49] PHP Warning:  Missing argument 1 for foo_shortcode() in
/home/path/to/header.php on line 23
[22-Jul-2009 08:52:49] done3

...发送到浏览器的输出(正确)是:

hi -- Hello World -- bye <img src="foobar.png" />

...但警告信息仍然产生。

编辑 2(7 月 22 日,星期三,上​​午 9:18)

然后我尝试了这个断言:

59: $str = $post->post_content;
60: assert( isset( $str ) );
61: $str = core_wp( 'foo_shortcode', $str );
62: assert( isset( $str ) );

... PHP 日志显示:

[22-Jul-2009 09:16:55] PHP Warning:  assert() [<a 
href='function.assert'>function.assert</a>]: Assertion failed in
/home/path/to/header.php on line 60
[22-Jul-2009 09:16:55] PHP Warning:  Missing argument 1 for foo_shortcode() in
/home/path/to/header.php on line 23

...但同样,输出是正确的,但仍然发出警告。

在我看来,PHP 最初没有设置 $str = $post->post-content,然后它运行 core-wp('foo-shortcode', $str) 并对自己说“哦!我真的需要to set $str = $post->post-content..." 返回并设置它,然后输出正确的数据。

【问题讨论】:

  • 感谢 Eineki,将 $args 初始化为数组并没有成功。
  • 你确定 $str = $post->post_content;评估为与 Null 不同的东西?
  • 是的,100% 确定,正如您在上面看到的 $post->post_content 包含一个字符串,“hi -- [hw] -- bye ”
  • 我想知道,任何安装了 WordPress 工作副本的人都能够重现这个吗?我目前已将代码粘贴到我的主题的 header.php 中。
  • 我无法重现此警告...但是我有一件小事:在您的情况下,$content 变量是一个数组,因此 str_replace() 将返回一个数组-这是故意的吗?

标签: php wordpress


【解决方案1】:

如果不是

$str = $post->post_content;

你暂时放:

$str = 'some string you believe to trigger the error';

错误是否仍然发生?如果是这样,那我们肯定会看core_wp()。如果不是,$post-&gt;post_content 可能是罪魁祸首。

编辑:要确定 $post-&gt;post_content 是否为 NULL,请添加以下行:

echo "post_content:";
var_dump($post->post_content);
$str = $post->post_content;

当错误发生时让我们知道它是否为 NULL。

EDIT2:既然您确定$post-&gt;post_contentnot NULL,请在您发布代码后立即尝试error_log("done");。警告是在“完成”之前还是之后出现?此外,将 error_log() 语句添加到所有相关函数的顶部,说明“调用 functionname”,这将帮助您确定它们是否被调用了两次。

【讨论】:

  • 罪魁祸首绝对是 $post->post_content 因为我完全按照您的建议尝试了 - 我设置了一个静态 $str 包含导致警告的文本​​,当我这样做时,警告消失了。这就是为什么我如此困惑。我不知道接下来要检查什么。
  • 这告诉我 $post->post_content 必须为 NULL
  • 嗨,乔希,因为我是 PHP 新手,你能解释一下 $post-post_content 如何在返回正确结果的情况下为 NULL 吗?这是我的主要问题。
  • 我不确定 $post->post_content 是从哪里来的……让我们从确定它是否为空开始。查看我编辑的帖子
  • 嗨乔希,这正是我所做的——事实上,我在之前和之后添加了 var_dump($post->post_content) 并且它是可用的。因此,我的结论是该函数似乎试图多次运行自身。
【解决方案2】:

我会尝试像使用 wrap 一样将 $args 初始化为 array()。 这应该可以解决您的问题。

function core_wp( $function, $a = NULL, $b = NULL )
{
  $wrap = array
  (
    'do_shortcode'                          => 'do_shortcode',
  );

  $args=array();
  if ( isset( $a ) ) $args[] = $a;
  if ( isset( $b ) ) $args[] = $b;

  return call_user_func_array( $wrap[ $function ], $args );
}

关于双重执行我不能说什么:您发布的代码对此不承担任何责任。

【讨论】:

  • 不行,我测试过,问题依旧。你确定 $str = $post->post_content;评估为不同于 Null 的东西?
猜你喜欢
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 2020-08-08
  • 2018-10-02
  • 1970-01-01
  • 2015-01-06
相关资源
最近更新 更多