To access a specific field method (to make it required or not for example) I use
$form->fields()['fieldname'] which works on localhost with wamp yet on the server throws this error:
Parse error: syntax error, unexpected '[' in (...)
I have PHP 5.3 on the server & because I reinstalled wamp & forgot to alter it back to 5.3, wamp runs PHP 5.4. So I guess this is the reason for the error.
How can I access an object method, which returns an array, by the array key with PHP 5.3?
So this code will work in PHP 5.4 & higher.
In order to make this work in PHP 5.3, you need to do one of the following:
- Use a temporary variable:
$temp = $form->fields() echo $temp['fieldname'];
2. Output the fields array as an object property rather than from a method:
…is perfectly valid.
3. Or, of course, you could upgrade your server to PHP 5.4. Bear in mind that 5.3 will be declared end-of-life relatively soon, now that 5.5 has been released, so you’ll be wanting to upgrade sooner or after anyway; maybe this is your cue to do? (and don’t worry approximately it; the upgrade path from 5.3 to 5.4 is pretty easy; there’s nothing really that will break, except things that were deprecated anyway)