戻る

メソッドの引数のデフォルト値にnullを設定することが禁止されています。
今までは下記のようなサンプルが可能でした。
function test(int $num, string $txt = null)
{
	if($txt == null)
	{
		$txt ="";
	}
...
}
しかし、php8.4からはデフォルト値にnullが禁止となります。
このため、下記の(1)および(2)をnull許容値型を使用して実装できます。
(1)php7.1でサポートされた?T構文
(2)php8.0でサポートされたUnion型
実際に上記サンプルを修正する場合は、
引数に使用している型宣言の前に「?」をつけます。
function test(int $num, ?string $txt)
{
	if($txt == null)
	{
		$txt ="";
	}
...
}
戻る
back

It is now prohibited to set the default value of a method argument to null.
Until now, the following sample was possible.
function test(int $num, string $txt = null)
{
	if($txt == null)
	{
		$txt ="";
	}
...
}
However, starting with php8.4, null is prohibited as the default value.
Therefore, (1) and (2) below can be implemented using a nullable value type.
(1)?T syntax supported in php7.1
(2)Union type supported in php8.0
If you actually modify the above sample,
add a "?" before the type declaration used in the argument.
function test(int $num, ?string $txt)
{
	if($txt == null)
	{
		$txt ="";
	}
...
}
back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。