「プログラミング」カテゴリーアーカイブ

PHP7.4.33からPHP8.3への移行ポイント:互換性・新機能・対応策まとめ

PHP7.4.33 は 2022年11月に 公式サポートが終了 (EOL) しており、セキュリティ更新も提供されません。
一方で、PHP8系は活発に開発が続けられており、最新の PHP8.3 ではパフォーマンス改善や新機能追加が進んでいます。
本記事では、PHP7.4.33からPHP8.3へ移行する際のポイントを 互換性・新機能・対応策 の3つの観点から整理し、具体的な修正例も比較表で紹介します。

WordPressなどで作成してるようなサイトだとついつい後回しになりがちなので注意しましょう。


1. 互換性のポイント

主な変更点

  • 非推奨機能の廃止create_function, ereg など)

  • 型システムの強化(引数や戻り値の厳格化)

  • Warning → Fatal Error への昇格(未定義配列キーなど)

  • 動的プロパティ禁止(PHP8.2以降)


2. PHP7.4 → PHP8.3 修正例(比較表)

項目PHP7.4での書き方PHP8.3での修正例エラー内容(PHP8.3)
動的プロパティclass User { }
$u = new User();
$u->name = "Alice"; // OK
class User { public string $name; }
$u = new User();
$u->name = "Alice"; // OK
Deprecated: Creation of dynamic property User::$name is deprecated
未定義配列キー$arr = [];
echo $arr["key"]; // Warning
$arr = [];
echo $arr["key"] ?? null; // Notice回避
Warning: Undefined array key "key"
create_functionの廃止$f = create_function('$x', 'return $x * 2;');
echo $f(3);
$f = fn($x) => $x * 2;
echo $f(3);
Fatal error: Uncaught Error: Call to undefined function create_function()
ereg → pregへの移行if (ereg("^[a-z]+$", $str)) { ... }if (preg_match("/^[a-z]+$/", $str)) { ... }Fatal error: Uncaught Error: Call to undefined function ereg()
implodeの引数順implode($array, ","); // Warningimplode(",", $array); // 正しい順序Deprecated: implode(): Passing glue after array is deprecated
型厳格化 (関数引数)function add($a, $b) { return $a + $b; }
echo add("1", 2); // 3 (暗黙変換)
function add(int $a, int $b): int { return $a + $b; }
echo add(1, 2); // OK
// 文字列を渡すと TypeError
Fatal error: Uncaught TypeError: add(): Argument #1 ($a) must be of type int, string given

 


3. 新機能の注目ポイント(PHP8.0〜8.3)


PHP8.0

  • JITコンパイル導入 → 数割の高速化

  • Union Types

  • Named Arguments

PHP8.1

  • Enums

  • Readonlyプロパティ

  • Fiber

PHP8.2

  • Readonlyクラス

  • Dynamic Properties 廃止

PHP8.3

  • Typed Class Constants

  • json_validate()

  • パフォーマンス改善(クラスロード周り)


4. 移行時の対応ステップ

  1. 検証環境構築(DockerやXAMPPなどでPHP8.3動作確認)

  2. composer update で依存パッケージをPHP8系対応版へ更新

  3. Deprecationログ確認(7.4でDeprecatedが出ていれば必ず修正)

  4. 段階的アップグレード(7.4 → 8.0 → 8.1 → 8.3 が安全)


まとめ

  • PHP7.4.33はEOL済みで脆弱性リスク大。

  • 移行時は 非推奨関数・型厳格化・動的プロパティ に注意。

  • 修正例を比較表で押さえておけば対応しやすい。

  • 最新のフレームワーク(Laravel, Symfony など)はPHP8.1以上必須が主流。

👉 早めにPHP8.3へ移行して、セキュリティ・パフォーマンス・開発効率を改善しましょう。