2

I have this code when I declaring function I have check if variable null or not null

with this code I have problem :

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

problem is :

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

how to use ? in php 5 , I use php 5.6

thanks advanced

paulo_dev
  • 43
  • 2
  • show your php 5.6 version code – Amit Gaud May 16 '17 at 08:49
  • About the best you can do with PHP 5.6 I think would be something like `private function addDepartmentField(FormInterface $form = null, Region $region = null) { ... }` - by giving it a default value of `null` you can pass `null` through as a parameter... or just leave it out of the method call all together of course. – CD001 May 16 '17 at 08:59

1 Answers1

2

Not possible in PHP 5.6. It's a new feature in PHP7.1. That's why you get this error.

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

Update

The only things are removing the ?, setting $region default to null (Region $region = null) or upgrading to PHP7.*. If you remove the ?, you have to pass a Region instance. That's in my opinion a better choice: avoiding null (What is the best alternative to null in object oriented programming?).

Community
  • 1
  • 1
schellingerht
  • 5,726
  • 2
  • 28
  • 56