将遗留代码“移植”到PHP是什么意思?

时间:2010-03-30 08:12:07

标签: php porting legacy-code

我本周进行了一次开卷测试,并且我已经收到通知,测试将是一项练习,其中提供了大量遗留代码并要求“移植”代码。

我理解开放式测试是什么以及它的要求(测试你的思考过程等)但是(这是一个长镜头)可以“移植”涉及什么?我对“移植”的概念含糊不清。

3 个答案:

答案 0 :(得分:1)

移植是指将代码从开发它的平台迁移到另一个平台 - 无论是Windows到Unix,还是ASP到PHP。

答案 1 :(得分:1)

移植是将代码从一个环境迁移到另一个环境 - 通常从一个操作系统迁移到另一个环境或从一个硬件平台迁移到另一个环境,但也可能来自不同的编程语言或同一编程语言的不同版本。 / p>

从上下文来看,我猜他们会为你提供旧的PHP版本的旧编码风格的PHP代码,并要求你更新代码,以便在具有现代编码标准的现代版PHP上正确运行。

答案 2 :(得分:1)

这可能意味着你得到一些(旧的)php4代码并且应该将它移植到php5 在这种情况下,代码应该使用设置error_reporting(E_ALL|E_STRICT)运行而不显示警告消息。还要检查每个功能/方法的描述是否包含“此功能已被弃用”注释/警告某处。
Imo可能的候选人是:会话,课程,ereg(posix正则表达式)甚至可能是register_globalsallow_call_time_pass_reference
也许你也应该发现“旧的”变通办法的使用,并用更新的功能替换它们。 E.g。

// $s = preg_replace('/foo/i', 'bar', $input);
// use php5's str_ireplace() instead
$s = str_ireplace('foo', 'bar', $input);

但这取决于你所涉及的主题。


E.g。 “将此php4代码移植到php5”:

<?php
class Foo {
  var $protected_v;

  function Foo($v) {
    $this->protected_v = $v;
  }

  function doSomething() {
    if ( strlen($this->protected_v) > 0 ) {
      echo $this->protected_v{0};
    }
  }
}

session_start();
if ( session_is_registered($bar) ) {
  $foo = new Foo($bar);
  $foo->doSomething();
}

答案可能是

<?php
class Foo {
  // php5 introduced visibility modifiers
  protected $v;

  // the "preferred" name of the constructor in php5 is __construct()
  // visibility modifiers also apply to method declarations/definitions
  public function __construct($v) {
    $this->v = $v;
  }

  public function doSomething() {
    if ( strlen($this->v) > 0 ) {
      // accessing string elements via {} is deprecated
      echo $this->v[0];
    }
  }
}

session_start();
// session_is_registered() and related functions are deprecated
if ( isset($_SESSION['bar']) ) {
  $foo = new Foo($_SESSION['bar']);
  $foo->doSomething();
}