PHP 5.3 accessing array key from object getter - php

I have a Form object $form. One of its variables is a Field object which represents all fields and 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 but on the server throws this error:
Parse error: syntax error, unexpected '[' in (...)
I have PHP 5.3 on the server and because I reinstalled wamp and forgot to change 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 is possible as of PHP 5.4, not 5.3
PHP.net:
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

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 and 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 later anyway; maybe this is your cue to do? (and don't worry about 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)

As mentioned array dereferencing is only possible in 5.4 or greater.
But you can save the object and later on access the fields:
$fields=$form->fields();
$value=$fields['fieldname']
...
AFAIK there is no other option.

Related

Why does dreamweaver show an error on this line when the code is valid

Why does Dreamweaver show a red error on this line
<?php $pt=$test->Terms()['Terms'];?>
The line is perfectly valid and runs, and does what it is supposed to do, and yet dreamweaver insists that it is an error.
I have a function returning a named array you see, and it works fine on the page.
You are directly accessing an array key of a function result. Array dereferencing of function results was implemented in PHP 5.4. See http://php.net/manual/en/language.types.array.php
I'd guess that the syntax checking of Dreamweaver uses a version of PHP < 5.4.
You should check array key existence before referencing to it

Having array identifiers on the end of a function in PHP?

I am having some some problems here. I am using XAMPP on a Windows box to test. I will be uploading the final product to a CentOS server.
I have this line of code:
$dbFunc->GetTemplateName($report['uat_template'])[0]['uat_name'];
On the Windows configuration, this works fine. However I get a parse error on the Unix box. It isn't happy with the [0] identifier.
If i do;
$temp = $dbFunc->GetTemplateName($report['uat_template']);
$temp = $temp[0];
echo $temp['uat_name'];
It works fine, but it's added 2 lines of code?
Is there any way to make the first way work on Unix?
Thanks
The first example in your question makes use of array dereferencing feature in PHP, which is only available for PHP 5.4+. If you're using an older PHP version, you will need to upgrade. If upgrading is not an option, you're stuck with the second method, I'm afraid.
In PHP 5.4 PHP added a function array dereferencing has been added, e.g. foo()[0].
So it looks like on CentOS you have >=PHP5.4 and on XAMPP <5.4
More info : http://php.net/manual/en/migration54.new-features.php

PHP 5.2 permitted object syntax to call array index?

We recently had a disaster and had to move our php web application from PHP Version 5.2.6-1+lenny16 to PHP Version 5.3.3-7+squeeze15 and found a seemingly important difference.
In our application, there were instances where we incorrectly called an array's index using object syntax:
echo $array->index;
However, 5.2.6 seemed to forgive this, and correctly treat it as if $array['index'] was written.
Upon further testing, what 5.2.6 is specifically doing is disagreeing with 5.3.3 as to whether $array->index is empty();
Here is the test code I've run on both servers:
<?php
echo phpversion() . '<br>';
$array = array(
'x' => 1,
'y' => 2
);
if (!empty($array->x))
{
echo "not empty";
}
else
{
echo "empty";
}
?>
Here are the two different outputs:
5.2.6-1+lenny16
not empty
5.3.3-7+squeeze15
empty
Naturally, there are now a few outbreaks of broken functionality because we were never alerted to these errors during development. Is there a way we can configure php 5.3 to permit this incorrect syntax while we take a bit more time to find all the incorrect instances of it?
I don't think it's a configuration issue, is it? Was something changed in the way empty() works in between versions?
I just have put your example code to a general test across PHP versions (test) and it shows that you are correct, there are differences:
From PHP 5.0.0 up to 5.2.11 (and also early 5.3.0 to 5.3.1), this "undefined property" was reported as not empty which does qualify as a flaw or bug.
The related change in 5.2.12 (17 Dec 2009) was (ref):
Fixed bug #50255 (isset() and empty() silently casts array to object). (Felipe)
Technically this is not a backwards incompatible change from PHP 5.2 to 5.3 because it was a flaw in both branches and also fixed in both. Those are harder to spot if you migrate, because the standard migration guide does not cover them. Instead you need to go through the changes of the software and look for notes and references to tickets.
So to answer your question: This is a configuration issue because the PHP version used counts as configuration. You changed the configuration and then you had the issue.
Also as the report shows, this is limited to empty() and isset(), not general object/array access. As you can imagine, if that would have been the case, you would have found much more reference about it.

PHP 5.4 to 5.3 Conversion

Are their any good resources when you are trying to get a PHP script written in a newer version to work on an older version; Specifically 5.4 to 5.3?
I have even checked out articles about the changes and I cannot seem to figure out what I am doing wrong.
Here is the error I am getting, at this moment:
Parse error: syntax error, unexpected '[' in Schedule.php on line 113
And the code it is referring to:
private static $GAMES_QUERY = array('season' => null, 'gameType' => null);
.....
public function getSeason(){
$test = array_keys(self::$GAMES_QUERY)[0]; //<<<<<<<<<< line:113
return($this->query[$test]);
}
Everything I have seen seems to say that 5.3 had self::, array_keys, and the ability to access arrays like that.
try...
$test = array_keys(self::$GAMES_QUERY);
$test = $test[0];
If I'm not mistaken, you can't use the key reference [0] in the same declaration in 5.3 like you can in 5.4 and javascript, etc.
That syntax was actually added in 5.4: http://docs.php.net/manual/en/migration54.new-features.php
So, you'll need a temporary variable to hold the result of the function, then access the index you want.
In versions lower than PHP 5.4 for the case you have you can make use of the list keywordDocs:
list($test) = array_keys(self::$GAMES_QUERY);
That works in PHP 5.4, too. But it does deal better with NULL cases than the new in PHP 5.4 array dereferencingDocs.

Is it correct to use end(explode($delim, $str)) in PHP 5.3?

An excerpt from the manual about the returned result:
The array. This array is passed by reference because it is modified by
the function. This means you must pass it a real variable and not a
function returning an array because only actual variables may be
passed by reference.
I use end(explode( and it works without any warnings or notices, but I have PHP 5.4 on the localhost and the product requirements are "PHP 5.3 or higher". I read about the function array dereferencing which had appeared in 5.4, but I thought it worked only for the square brackets array element dereferencing.
Could somebody please clear the matter for me? Or at least give me some links about inner workings of PHP. There is a handy technical reference for JavaScript, I could make use of something like it for PHP.
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
it creates temporary variable automatically

Categories