PHP回调函数的可变范围

时间:2021-09-28 20:24:56

In response to another question I asked about regular expressions, I was told to use the preg_replace_callback function (PHP regex templating - find all occurrences of {{var}}) as a solution to my problem. This works great, but now I have a question relating to variable scope in callback functions.

在回答我询问正则表达式的另一个问题时,我被告知使用preg_replace_callback函数(PHP regex模板 - 查找所有出现的{{var}})作为我的问题的解决方案。这很好用,但现在我有一个与回调函数中的变量范围有关的问题。

The function that parses the text is part of a class, but the data that I want to use is stored locally in the function. However, I have found that I cannot access this data from inside my callback function. Here are the ways that I have tried so far:

解析文本的函数是类的一部分,但我想要使用的数据本地存储在函数中。但是,我发现我无法从回调函数中访问此数据。以下是我迄今为止尝试过的方法:

  • Implement the callback as a private class function, passing '$this->callback_function' as the callback parameter (doesn't work, php has a fatal error)
  • 将回调实现为私有类函数,将'$ this-> callback_function'作为回调参数传递(不起作用,php有致命错误)

  • Implement the callback inside the function that uses it (see example below) but this didn't work either because $newData is not in scope inside callback_function
  • 在使用它的函数内部实现回调(参见下面的示例),但这不起作用,因为$ newData不在callback_function内的范围内

Any ideas as to how I can access $newData inside my callback function, preferably without using globals?
Many thanks.

关于如何在回调函数中访问$ newData的任何想法,最好不使用全局变量?非常感谢。

Example below for the second attempt (doesn't format properly when I put it after the bullet point)

第二次尝试的示例(当我把它放在项目符号点后没有正确格式化)

public function parseText( $newData ) {
  ...
  function callback_function( $matches ) {
    ...  //something that uses $newData here
  }
  ...
  preg_replace_callback( '...', 'callback_function', $textToReplace );
}

2 个解决方案

#1


  • Implement the callback as a private class function, passing '$this->callback_function' as the callback parameter (doesn't work, php has a fatal error)
  • 将回调实现为私有类函数,将'$ this-> callback_function'作为回调参数传递(不起作用,php有致命错误)

preg_replace_callback( '...', 'callback_function', $textToReplace );

preg_replace_callback('...','callback_function',$ textToReplace);

Change your call to be preg_replace_callback ('...', array($this, 'callback_function'), $textToReplace); while callback_function is a private method in your class.

将您的调用更改为preg_replace_callback('...',array($ this,'callback_function'),$ textToReplace);而callback_function是您班级中的私有方法。

<?php

class PregMatchTest
{

    private callback_function ($matches)
    {
        // ......
    }

    public function parseText ($newData)
    {
        // ....

        preg_replace_callback( '...', array($this, 'callback_function'), $textToReplace );
    }

}

?>

#2


I don't think it's possible without using globals, maybe just set it on the $_GLOBALS array and then unset it if you wish.

我认为不使用全局变量是可能的,也许只需将它设置在$ _GLOBALS数组上,然后根据需要取消设置。

#1


  • Implement the callback as a private class function, passing '$this->callback_function' as the callback parameter (doesn't work, php has a fatal error)
  • 将回调实现为私有类函数,将'$ this-> callback_function'作为回调参数传递(不起作用,php有致命错误)

preg_replace_callback( '...', 'callback_function', $textToReplace );

preg_replace_callback('...','callback_function',$ textToReplace);

Change your call to be preg_replace_callback ('...', array($this, 'callback_function'), $textToReplace); while callback_function is a private method in your class.

将您的调用更改为preg_replace_callback('...',array($ this,'callback_function'),$ textToReplace);而callback_function是您班级中的私有方法。

<?php

class PregMatchTest
{

    private callback_function ($matches)
    {
        // ......
    }

    public function parseText ($newData)
    {
        // ....

        preg_replace_callback( '...', array($this, 'callback_function'), $textToReplace );
    }

}

?>

#2


I don't think it's possible without using globals, maybe just set it on the $_GLOBALS array and then unset it if you wish.

我认为不使用全局变量是可能的,也许只需将它设置在$ _GLOBALS数组上,然后根据需要取消设置。