2017-05-16 6 views
2

で関数を宣言して、私はこのコードを持っている私は、私は問題を抱えている。このコードであれば、変数NULLまたはNOT NULLチェックはPHP5

を確認しています

private function addDepartementField(FormInterface $form, ?Region $region) { .... } 

問題は次のとおりです。

Parse Error: syntax error, unexpected '?', expecting variable (T_VARIABLE)

使用方法は? PHP 5で、私はPHPを使用して5.6

おかげでPHP 5.6で

+0

あなたのPHP 5.6バージョンコードを表示 –

+0

PHP 5.6でできることについて私は 'private function addDepartmentField(FormInterface $ form = null、Region $ region = null){...}'のようになると思います。デフォルト値の 'null'を与えると、パラメータとして' null'を渡すことができます。または、もちろんそれをメソッド呼び出しから外すこともできます。 – CD001

答えて

2

ない可能性を進めました。 PHP7.1の新機能です。そのため、このエラーが発生します。

Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively. http://php.net/manual/en/migration71.new-features.php

更新

唯一のものはヌルに$regionデフォルト(Region $region = null)を設定またはPHP7.*へのアップグレード、?を削除しています。 ?を削除する場合は、Regionインスタンスを渡す必要があります。それは私の意見ではより良い選択です:nullを避けるWhat is the best alternative to null in object oriented programming?)。

+1

私の例の解決策は何ですか? –

+0

私の更新された回答を参照してください。 nullを避けることをお勧めします。 – schellingerht