开发者

What does it mean to "port" legacy code to PHP?

开发者 https://www.devze.com 2022-12-25 15:43 出处:网络
I have a open-book test this week and I\'ve been notified that the test will开发者_开发技巧 be an exercise whereby a chunk of legacy code is provided and a requirement to \'port\' the code.

I have a open-book test this week and I've been notified that the test will开发者_开发技巧 be an exercise whereby a chunk of legacy code is provided and a requirement to 'port' the code.

I understand what a open-book test is and the requirement of it (to test your thought process etc) but (it's a long shot) what could 'porting' involve? I have a vague idea of what 'porting' is.


Porting refers to migrating code from the platform on which it was developed on to another platform - be it Windows to Unix, or ASP to PHP.


Porting is the migration of code from one environment to another -- often from one OS to another or from one hardware platform to another, but also potentially from a different programming language or from a different version of the same programming language.

From the context, I'm guessing that they will give you PHP code written in an old coding style for an old PHP version and ask you to update the code to run correctly on a modern version of PHP with modern coding standards.


It could mean that you get some (old) php4 code and are supposed to port it to php5.
In that case the code should run with the setting error_reporting(E_ALL|E_STRICT) without warning messages. Also check the description of each function/method whether it contains a "This function has been DEPRECATED" note/warning somewhere.
Imo likely candidates are: sessions, classes, ereg (posix regular expressions) maybe even register_globals and allow_call_time_pass_reference.
Maybe you're also supposed to spot the usage of "old" workarounds and to replace them with newer functions. E.g.

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

But that depends on the topics you've covered.


E.g. "Port this php4 code to 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();
}

And the answer could be

<?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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消