PHP函数调用两次,有趣的警告消息

PHP函数调用两次,有趣的警告消息,php,wordpress,Php,Wordpress,最终编辑:我已经将这个疯狂的问题总结并简化为一个新问题,因此我们可以关闭此问题或任何必要的问题来标记它已解决。再次感谢 新建: 你能告诉我你对这件事的看法吗?也许你可以再做一次: 当前,$post->post\u内容变量包含: "before <img src="/path/to/valid_img.gif" /> after" 下面是core-wp()和foo-shortcode(): 首先,为什么代码工作得很好,然后同时又试图再次运行自己 关于包含无效img的$str,我怀疑这

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

新建:

你能告诉我你对这件事的看法吗?也许你可以再做一次:

当前,$post->post\u内容变量包含:

"before <img src="/path/to/valid_img.gif" /> after"
下面是core-wp()和foo-shortcode():

首先,为什么代码工作得很好,然后同时又试图再次运行自己

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

编辑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
。。。发送到浏览器的输出(正确)为:

。。。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
[22-Jul-2009 09:16:55]PHP警告:assert()[]:在中断言失败
/第60行的home/path/to/header.php
[22-Jul-2009 09:16:55]PHP警告:中缺少foo_shortcode()的参数1
/第23行的home/path/to/header.php
。。。但同样,输出是正确的,但仍然发出警告


在我看来,PHP最初并没有设置$str=$post->post内容,然后它运行核心wp('foo shortcode',$str),并对自己说:“哦!我真的需要设置$str=$post->post内容…”返回并设置它,然后输出正确的数据。

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

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 );
}
关于双重执行,我不能说什么:您发布的代码对此不负责。

If而不是

$str = $post->post_content;
你临时提出:

$str = 'some string you believe to trigger the error';
错误是否仍然发生?如果是这样的话,我们就可以肯定地看一下
core\u wp()
。否则,
$post->post\u内容可能是罪魁祸首

编辑:要确定
$post->post\u content
是否为空,请添加以下行:

echo "post_content:";
var_dump($post->post_content);
$str = $post->post_content;
当错误发生时,让我们知道它是否为NULL


EDIT2:既然您确定
$post->post\u内容
不是空的,请尝试
错误日志(“完成”)在发布代码后立即执行。警告显示在“完成”之前还是之后?另外,将error_log()语句添加到所有相关函数的顶部,声明“called functionname”,这将帮助您确定它们是否被调用了两次。

谢谢Eineki,将$args初始化为数组并没有起到作用。您确定$str=$post->post_content;计算为与Null不同的值?是的,100%确定,正如您在上面看到的$post->post_内容包含一个字符串,“嗨--[hw]--bye”我想知道,安装了WordPress工作副本的任何人都可以复制这个吗?我目前已将代码粘贴到主题的header.php中。我无法重现此警告。。。但我有一点:在您的例子中,$content变量是一个数组,str_replace()将返回一个数组,因此-这是故意的吗?不,我已经测试过了,问题仍然存在。您确定$str=$post->post\u内容;计算为与Null不同的值?罪魁祸首肯定是$post->post_内容,因为我完全按照您的建议进行了尝试--我设置了一个静态$str,其中包含导致警告的文本,当我这样做时,警告消失了。这就是为什么我如此困惑。我不知道下一步要检查什么。这就告诉我$post->post\u内容必须为空。嘿,Josh,因为我是PHP新手,你能解释一下即使返回正确的结果,$post-post\u内容怎么可能为空吗?这是我的主要问题。我不确定$post->post\u内容来自哪里。。。让我们先确定它是否为null。查看我编辑的postHi Josh,这正是我所做的——事实上,我在前后添加了var_dump($post->post_content),并且它是可用的。因此,我的结论是,函数似乎试图多次运行自己。
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 );
}
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" />
59: $str = $post->post_content;
60: assert( isset( $str ) );
61: $str = core_wp( 'foo_shortcode', $str );
62: assert( isset( $str ) );
[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
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;
$str = 'some string you believe to trigger the error';
echo "post_content:";
var_dump($post->post_content);
$str = $post->post_content;