PHP 5.4 to 5.3 Conversion - php

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.

Related

PHP gives unexpected [ - but why?

having strange trouble with this:
date_parse('2014-08-26')['hour']
It works on a php command line.
But this throws an error:
if(date_parse('2014-08-26')['hour']){ echo "works";}
syntax error, unexpected '['
Why is that? The result should be any number between 0 and 23 or nothing, nothing would it be in this case.
EDIT: yes, I am on PHP 5.3.14
Please note that versions of PHP older than 5.4 can not access array attributes from a functions return value directly. You have to store the functions return value in a new variable and access the array from that variable.
So this code
if(date_parse('2014-08-26')['hour']){ echo "works";}
only works on PHP 5.4 and higher. If you run a PHP version lower then 5.4 your code should look like this:
$parseResult = date_parse('2014-08-26');
if($parseResult['hour']) { echo "works"; }
You must be using below 5.4 version of PHP. If so than please use the latest version of php as the version below 5.4 does not allow brackets [].
Before version 5.4, PHP didn't allow you to dereference elements of arrays returned from function calls without using a temporary variable:
http://php.net/manual/en/language.types.array.php#example-102
$cond = date_parse('2014-08-26')['hour'];
if($cond)
{
echo "works";
}
Hope this works

PHP array declaration standards

I'm not a usual php user but till now I always used to declare arrays in this way:
$arr = ["id" => 15,"val" => 13];
In my local xampp (PHP Version 5.5.9) environment this worked fine, but on server (PHP Version 5.3.28) this code fails giving:
PHP Parse error: syntax error, unexpected '[' in /web/htdocs/site.sit/home/pdo.php on line 24
I switched the declaration to this and everything is ok,
$arr = array("id" => 15,"val" => 13)
But I want to understand why this error occurred
As documentation states it is not a matter of deprecated code and I see that the first example is using my first array declaration with the comment note
// as of PHP 5.4
What does it means?
Anyway I suspect that its a problem related with some sort strict mode.
array() has always been the way to declare arrays in PHP since before the dawn of time. In PHP 5.4, the shorter [] has been introduced, simply because it's shorter and many other languages use it too. [] doesn't work in 5.3 or below. TFM documents that.

PHP cannot automatically use function result as array (in PHP 5.3)

When I try to run something like
getParent($child)[0]->user
I get the following error:
PHP Parse error: syntax error, unexpected '[', expecting ')' in /.....
The problem can be avoided if I do something like this:
$get_parent = getParent($child);
$parent = $get_parent[0]->user;
Is there a better way to do in php 5.3
No, before PHP5.4, you have to do that.
Array dereferencing comes from PHP 5.4.
But if getParent return an object which implement the ArrayAccess interface, you could chain it with:
$parent = getParent($child)->offsetGet(0)->user;
If it just return an array, then a temp variable is necessary.
Actually, there is a way to achieve that without temporary variable. But I definitely wouldn't recommend to use it (because, yes, it's one-liner and it's not using temporary variable, but no - it's not readable):
function getParent($child=null)
{
//mock:
return array(
(object)(array('user'=>'foo', 'data'=>'fee')),
(object)(array('user'=>'bar', 'data'=>'bee')),
);
};
//array(null) will have 1 key, 0;
//however, to get another offset N, use array(N => null) instead
$result = array_shift(array_intersect_key(getParent('baz'), array(null)))->user;
-so use temporary variable if your version in <5.4
Where it may be helpful - is in debugger, where you're forced to use "one-liners" to check some expression(s)
There is no better way to do it when using PHP version < 5.4.
If you still want to use one line instead of 2 and you always want to get the first element, you can try the following
echo reset(getParent($child))->user;
This reset the array pointer to 0 and returns the value.

Customzing Eventbrite list, I get unexpected T_FUNCTION, but it works locally [duplicate]

I have a certain piece of code that I'm trying to use with PHP Version 5.2.14 . Is it incompatible?? I run the following,
jailshell-3.2$ php -l /XYZ/functions.php
And it gives:
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /XYZ/functions.php on line 2115
Errors parsing /XYZ/functions.php
The code is:
2114 $range = array_map(
2115 function (DatePeriod $p) use ($vt2) {
2116 $res = array();
Your code uses anonymous functions which were supported in PHP 5.3. So, you need PHP 5.3 to get it to working. Upgrade your server's PHP installation.
Anonymous functions, also known as closures, allow the creation of functions which have no specified name.
You are using anonymous functions which are available since PHP 5.3.0.
To resolve this you can upgrade your PHP as suggested in other answer.
Alternatively you can define the function outside array_map and then use that function name in the call to array_map
From the php manual on Anonymous Functions:
Note: Anonymous functions are available since PHP 5.3.0.
prior to 5.3.0, do it like this:
$range = array_map( "name_of_function_to_call", $myArray );
I think the lambda style function is not yet implemented in 5.2
use create_function or just create the function and pass it the function name in array_map.

PHP 5.3 accessing array key from object getter

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.

Categories