PHP - PHP 8.2 → 8.4 - ソース修正


 クラウディア


1. 概要
2. 出力の意味
3. 修正
4. 参考サイト

1. 概要

 前ページで、エラーみたいになっちょるので、ソースを修正します。

2. 出力の意味

 出力されているメッセージ。

PHP Deprecated:  App\Lib\contents::getAbsolute(): Implicitly marking parameter $relate as nullable is deprecated, the explicit nullable type must be used instead in /path/app/Lib/contents.php on line 574
PHP Deprecated:  App\Lib\Mobile_Detect::__construct(): Implicitly marking parameter $headers as nullable is deprecated, the explicit nullable type must be used instead in /path/app/Lib/Mobile_Detect.php on line 890
 を「Google」大先生に翻訳してもらいましたらば、こういうことのようです。

PHP 非推奨: App\Lib\contents::getAbsolute(): 暗黙的にパラメータ $relate を null 許容としてマークすることは非推奨です。/path/app/Lib/contents.php の 574 行目では、明示的に null 許容型を使用する必要があります。
PHP 非推奨: App\Lib\Mobile_Detect::__construct(): 暗黙的にパラメータ $headers を null 許容としてマークすることは非推奨です。/path/app/Lib/Mobile_Detect.php の 890 行目では、明示的に null 許容型を使用する必要があります。
 なるほど、どちらもいただいたソースだったり、作成したソースなので、変更しても問題ないです。

3. 修正

 「PHP 8.2」では、パラメータの省略値を。

function func(string $str = null)
 てな形式で書くことが許容されていましたが。  「PHP 8.2」では、これは許可されず。

function func(?string $str = null)
 と、方の前に「?」をつけるのが「nullable」な書き方であるそうです。  なので、「contents.php」は。

        public function getAbsolute(string $relate = null): string
 を下記へ。

        public function getAbsolute(?string $relate): string
 「Mobile_Detect.php」は。

    public function __construct(
        array $headers = null,
        $userAgent = null
    ) {
 を下記へ。

    public function __construct(
        ?array $headers = null,
        ?string $userAgent = null
    ) {
 これで、なにも出力されなくなりました。

4. 参考サイト

 本ページは、「ChatGPT」くんを参考にさせていただきました。

AbemaTV 無料体験