[英]What does it mean to “port” legacy code to PHP?
我本周有一次開卷測試,並且已經通知我該測試是一種練習,其中將提供大量舊代碼,並要求“移植”代碼。
我了解公開考試是什么以及它的要求(以測試您的思維過程等),但是(很長的時間)“移植”可能涉及什么? 我對什么是“移植”有一個模糊的想法。
移植是指將代碼從開發平台遷移到另一個平台-從Windows到Unix,或者從ASP到PHP。
移植是代碼從一種環境到另一種環境的遷移-通常是從一種操作系統到另一種操作系統或從一種硬件平台到另一種硬件平台的遷移,但也有可能是從另一種編程語言或同一編程語言的另一種版本遷移而來。
從上下文來看,我猜測它們將為您提供以舊編碼樣式編寫的PHP代碼,以用於舊PHP版本,並要求您更新代碼以在具有現代編碼標准的現代PHP版本上正確運行。
這可能意味着您得到了一些(舊的)php4代碼,並且應該將其移植到php5中。
在這種情況下,代碼應在設置error_reporting(E_ALL|E_STRICT)
情況下運行而沒有警告消息。 還要檢查每個功能/方法的說明是否包含“此功能已被棄用”注釋/警告。
可能的候選對象包括:會話,類,ereg(posix正則表達式)甚至register_globals和allow_call_time_pass_reference 。
也許您還應該找出“舊”解決方法的用法,並用較新的功能替換它們。 例如
// $s = preg_replace('/foo/i', 'bar', $input);
// use php5's str_ireplace() instead
$s = str_ireplace('foo', 'bar', $input);
但這取決於您所涵蓋的主題。
例如“將此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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.