Symfony 1.4 在 php 5.5 中使用已弃用的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18077276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:56:59  来源:igfitidea点击:

Symfony 1.4 using deprecated functions in php 5.5

phppreg-replacesymfony-1.4preg-replace-callback

提问by mika

I recently upgraded PHP from version 5.3.27 to 5.5.0. Everything is working fine in my Symfony 2.3.2 project, and I can enjoy the latest PHP functionalities.

我最近将 PHP 从 5.3.27 版本升级到 5.5.0。在我的 Symfony 2.3.2 项目中一切正常,我可以享受最新的 PHP 功能。

Now when I am going back to my other Symfony 1.4.16 project, I get a PHP error about preg_replace being deprecated with the /e modifier.

现在当我回到我的另一个 Symfony 1.4.16 项目时,我收到一个关于 preg_replace 被 /e 修饰符弃用的 PHP 错误。

I can find no reference about this error in the forums: Has anyone had this problem before ? Is there any kind of patch that I could apply out of the box ? Is an upgrade to Symfony 1.4.20 going to fix this issue ?

我在论坛中找不到有关此错误的参考: 以前有人遇到过这个问题吗?有什么补丁可以开箱即用吗?升级到 Symfony 1.4.20 会解决这个问题吗?

The error message goes like this:

错误信息是这样的:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /myproject/lib/vendor/symfony/lib/response/sfWebResponse.class.php on line 409

不推荐使用:preg_replace():不推荐使用 /e 修饰符,在第 409 行的 /myproject/lib/vendor/symfony/lib/response/sfWebResponse.class.php 中使用 preg_replace_callback 代替

One way to go may be to modify the code as recommended in the message, and in the manual. How can I change my preg_replace expression to a preg_replace_callback call ?

一种方法可能是按照消息和手册中的建议修改代码。如何将我的 preg_replace 表达式更改为 preg_replace_callback 调用?

Any help / hint will be very welcome.

任何帮助/提示将非常受欢迎。

EDIT:

编辑:

To this date, there is no patch for this (and Symfony 1.4.20 does not address the issue). The solution is to replace failing calls to preg_replace with corresponding call to preg_replace_callback in the sourche, which is easily done in the sfWebResponse class (thanks for the hint Jon). Now next failing occurrenceis slightly more complex, unfortunately... And on the other hand, we probably would have to grep for preg_replace uses with /e option in order to find out where Symfony is likely to break. Which gives quite a few results :o

到目前为止,还没有补丁(Symfony 1.4.20 没有解决这个问题)。解决方案是用源中对 preg_replace_callback 的相应调用替换对 preg_replace 的失败调用,这在 sfWebResponse 类中很容易完成(感谢 Jon 的提示)。不幸的是,现在下一次失败的情况稍微复杂一些......另一方面,我们可能必须使用 /e 选项 grep 使用 preg_replace 以找出 Symfony 可能会中断的地方。这给出了很多结果:o

So... My conclusion would be that Symfony 1.4 users would better not upgrade PHP to version 5.5 until some serious patch comes out. What do you think ? Any alternative ?

所以...我的结论是,Symfony 1.4 用户最好不要将 PHP 升级到 5.5 版,直到一些严重的补丁出现。你怎么认为 ?任何选择?

回答by flm

The errors do not show up in prod unless you have enabled debug in index.php. It' s also possible to remove them in dev by unsetting the E_DEPRECATED flag in settings.yml:

除非您在index.php 中启用了调试,否则错误不会出现在 prod 中。也可以通过在settings.yml 中取消设置E_DEPRECATED 标志来在 dev 中删除它们:

dev:
  .settings:
    error_reporting:  <?php echo ((E_ALL | E_STRICT) ^ E_DEPRECATED)."\n" ?>

回答by Jon

Basically what you have to do is take the replacement argument from the preg_replacecall and factor it out into a proper PHP expression, then make that expression the body of a function that will be used as the callback to the equivalent preg_replace_callbackcall.

基本上,您需要做的是从preg_replace调用中取出替换参数并将其分解为适当的 PHP 表达式,然后将该表达式作为函数的主体,该函数将用作等效preg_replace_callback调用的回调。

In your case the relevant codeis

在您的情况下,相关代码

return preg_replace('/\-(.)/e', "'-'.strtoupper('\1')", /* input */)

So you would do that as

所以你会这样做

$callback = function($matches) {
    return '-'.strtoupper($matches[1]);
};

return preg_replace_callback('/\-(.)/', $callback, /* input */)

As you can see the callback code is the same as the original replace expression, the only difference being that references such as \\1are replaced with array accesses like $matches[1].

如您所见,回调代码与原始替换表达式相同,唯一的区别是诸如引用之类的引用\\1被替换为诸如$matches[1].

回答by mika

All in all, the best solution is to avoid upgrading PHP to version 5.5, as it is no more compatible with Symfony 1.4

总而言之,最好的解决方案是避免将 PHP 升级到 5.5 版,因为它不再与 Symfony 1.4 兼容

If you have both Symfony 2 and 1.4 versions in a development environment, you may want to be able to switch your PHP version, as nicely described here.

如果您同时Symfony的2和1.4版本在开发环境中,您可能希望能够切换你的PHP版本,很好地描述了这里

If you really need to, it is possible to setup two different versions of PHP running on the same Apache server at the same time: this will need some more configuration, the above link explains that too.

如果您确实需要,可以设置两个不同版本的 PHP 同时运行在同一个 Apache 服务器上:这将需要更多配置,上面的链接也解释了这一点。

Alternative HOT FIX:

替代热修复:

With a couple of updates in the Symfony code, I can get most of my webpages running in dev. Of course, it would be dangerous to apply this in production, as the "deprecated" error may turn up again at any time, arising from another Symfony library.

通过 Symfony 代码的一些更新,我可以让我的大部分网页在 dev 中运行。当然,在生产中应用它是危险的,因为“弃用”错误可能随时再次出现,由另一个 Symfony 库引起。

In myproject/lib/vendor/symfony/lib/response/sfWebResponse.class.php on line 409, I have now (commented code is original Symfony code):

在第 409 行的 myproject/lib/vendor/symfony/lib/response/sfWebResponse.class.php 中,我现在有(注释代码是原始 Symfony 代码):

  protected function normalizeHeaderName($name)
  {
    // return preg_replace('/\-(.)/e', "'-'.strtoupper('\1')", strtr(ucfirst(strtolower($name)), '_', '-'));    

    return preg_replace_callback(
                  '/\-(.)/', 
                  function ($matches) {
                    return '-'.strtoupper($matches[1]);
                  }, 
                  strtr(ucfirst(strtolower($name)), '_', '-')
        );
  }

And in myproject/lib/vendor/symfony/lib/util/sfToolkit.class.php on line 362 we get:

在第 362 行的 myproject/lib/vendor/symfony/lib/util/sfToolkit.class.php 中,我们得到:

  public static function pregtr($search, $replacePairs)
  {
    // return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
    foreach($replacePairs as $pattern => $replacement)
        $search = preg_replace_callback(
                    $pattern, 
                    function ($matches) use ($replacement){
                        if(array_key_exists(1, $matches)){ $replacement = str_replace("\1", $matches[1], $replacement);}
                        if(array_key_exists(2, $matches)){ $replacement = str_replace("\2", $matches[2], $replacement);}
                        return $replacement;
                    }, 
                    $search
                );
    return $search;
  }

Use at your own risks :)

使用风险自负:)

回答by macatapichon

FIX for normalizeHeaderName method in /lib/vendor/symfony/lib/response/sfWebResponse.class.php on line 407

在第 407 行的 /lib/vendor/symfony/lib/response/sfWebResponse.class.php 中修复 normalizeHeaderName 方法

protected function normalizeHeaderName($name)
{
  //return preg_replace('/\-(.)/e', "'-'.strtoupper('\1')", 
  strtr(ucfirst(strtolower($name)), '_', '-');
  return str_replace(array('\'\'','\'\'','\'\'', '\'\'', '', ''),array('$matches[1].$matches[3]','$matches[2].$matches[4]','$matches[1]','$matches[2]','$matches[1]','$matches[2]'),
$name);
}

FIX for pregtr method in /lib/vendor/symfony/lib/util/sfToolkit.class.php on line 360

在第 360 行的 /lib/vendor/symfony/lib/util/sfToolkit.class.php 中修复 pregtr 方法

public static function pregtr($search, $replacePairs){
  // return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
  foreach($replacePairs as $pattern => $replacement)
  {
    if (preg_match('/(.*)e$/', $pattern, $matches))
    {
      $pattern = $matches[1];
      $search = preg_replace_callback($pattern, function ($matches) use ($replacement) {
        preg_match("/('::'\.)?([a-z]*)\('\\([0-9]{1})'\)/", $replacement, $match);
        return ($match[1]==''?'':'::').call_user_func($match[2], $matches[$match[3]]);
      }, $search);
    }
    else
    {
      $search = preg_replace($pattern, $replacement, $search);
    }
  }
  return $search;
}

回答by Kevin

There is a community version of Symfony that maintains and patches the older code:

Symfony 有一个社区版本,用于维护和修补旧代码:

https://github.com/LExpress/symfony1

https://github.com/LExpress/symfony1

回答by sadikoff

Alternative FIX for pregtrmethod in /lib/vendor/symfony/lib/util/sfToolkit.class.phpon line 360

替代FIX为pregtr方法在/ lib /供应商/ symfony的/ LIB / util的/ sfToolkit.class.php上线360

public static function pregtr($search, $replacePairs)
{
  // return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
  foreach($replacePairs as $pattern => $replacement)
  {
    if (preg_match('/(.*)e$/', $pattern, $matches))
    {
      $pattern = $matches[1];
      $search = preg_replace_callback($pattern, function ($matches) use ($replacement) {
        preg_match("/('::'\.)?([a-z]*)\('\\([0-9]{1})'\)/", $replacement, $match);
        return ($match[1]==''?'':'::').call_user_func($match[2], $matches[$match[3]]);
      }, $search);
    }
    else
    {
      $search = preg_replace($pattern, $replacement, $search);
    }
  }
  return $search;
}

回答by ImLeo

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in lib/vendor/symfony/…This changelog will solve the problem for all symfony 1.4.x. Tested on Symfony 1.4.20
---
 lib/vendor/symfony/lib/command/sfCommandManager.class.php     |  4 +++-
 lib/vendor/symfony/lib/form/addon/sfFormObject.class.php      |  2 +-
 lib/vendor/symfony/plugins/sfDoctrinePlugin/lib/form/sfFormFilterDoctrine.class.php  |  2 +-
 lib/vendor/symfony/plugins/sfPropelPlugin/lib/form/sfFormFilterPropel.class.php      |  2 +-
 lib/vendor/symfony/lib/response/sfWebResponse.class.php       |  2 +-
 lib/vendor/symfony/lib/util/sfInflector.class.php             |  5 +----
 lib/vendor/symfony/lib/util/sfToolkit.class.php               | 11 +++++++++++
 7 files changed, 19 insertions(+), 9 deletions(-)

lib/vendor/symfony/lib/command/sfCommandManager.class.php
@@ -108,7 +108,9 @@ class sfCommandManager
     else if (!is_array($arguments))
     {
       // hack to split arguments with spaces : --test="with some spaces"
-      $arguments = preg_replace('/(\'|")(.+?)\1/e', "str_replace(' ', '=PLACEHOLDER=', '\2')", $arguments);
+      $arguments = preg_replace_callback('/(\'|")(.+?)\1/', function($matches) {
+         return str_replace(' ', '=PLACEHOLDER=', $matches[2]);
+     }, $arguments);
       $arguments = preg_split('/\s+/', $arguments);
       $arguments = str_replace('=PLACEHOLDER=', ' ', $arguments);
     }

lib/vendor/symfony/lib/form/addon/sfFormObject.class.php
@@ -278,6 +278,6 @@ abstract class sfFormObject extends BaseForm

   protected function camelize($text)
   {
-    return preg_replace(array('#/(.?)#e', '/(^|_|-)+(.)/e'), array("'::'.strtoupper('\1')", "strtoupper('\2')"), $text);
+    return sfToolkit::camelize($text);
   }
 }

lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormFilterDoctrine.class.php
@@ -323,7 +323,7 @@ abstract class sfFormFilterDoctrine extends sfFormFilter

   protected function camelize($text)
   {
-    return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\1')", '/(^|_|-)+(.)/e' => "strtoupper('\2')"));
+    return sfToolkit::camelize($text);
   }

   protected function getTable()

lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/form/sfFormFilterPropel.class.php
@@ -263,6 +263,6 @@ abstract class sfFormFilterPropel extends sfFormFilter

   protected function camelize($text)
   {
-    return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\1')", '/(^|_|-)+(.)/e' => "strtoupper('\2')"));
+       return sfToolkit::camelize($text);
   }
 }

lib/vendor/symfony/lib/response/sfWebResponse.class.php
@@ -406,7 +406,7 @@ class sfWebResponse extends sfResponse
    */
   protected function normalizeHeaderName($name)
   {
-    return preg_replace('/\-(.)/e', "'-'.strtoupper('\1')", strtr(ucfirst(strtolower($name)), '_', '-'));
+    return preg_replace_callback('/\-(.)/', function ($matches) { return '-'.strtoupper($matches[1]); }, strtr(ucfirst(strtolower($name)), '_', '-'));
   }

   /**

lib/vendor/symfony/lib/util/sfInflector.class.php
@@ -28,10 +28,7 @@ class sfInflector
   public static function camelize($lower_case_and_underscored_word)
   {
     $tmp = $lower_case_and_underscored_word;
-    $tmp = sfToolkit::pregtr($tmp, array('#/(.?)#e'    => "'::'.strtoupper('\1')",
-                                         '/(^|_|-)+(.)/e' => "strtoupper('\2')"));
-
-    return $tmp;
+    return sfToolkit::camelize($tmp);;
   }

   /**

lib/vendor/symfony/lib/util/sfToolkit.class.php
@@ -608,4 +608,15 @@ class sfToolkit

     return set_include_path(join(PATH_SEPARATOR, $paths));
   }
+
+   public static function camelize($text)
+   {
+       if (preg_match('#/(.?)#', $text, $matches)) {
+           $text = str_replace($matches[0], '::'.strtoupper($matches[1]), $text);
+       }
+       if (preg_match('/(^|_|-)+(.)/', $text, $matches)) {
+           $text = str_replace($matches[0], strtoupper($matches[2]), $text);
+       }
+       return $text;
+   }
 }
--