I have a Form object $form. One of its variables is a Field object which represents all fields & is an array (e.g $this->field['fieldname']
). The getter is $form->fields()
.
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?
Array dereferencing as described in the question is a feature that was only added in PHP 5.4. PHP 5.3 cannot do this.
echo $form->fields()['fieldname']
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'];
Output the fields array as an object property rather than from a method:
ie this….echo $form->fields['fieldname']
…is perfectly valid.
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)
No comments:
Post a Comment