Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] I this a bug in PHP 7.1.9?

Posted by Jefferson Gonzalez 
Jefferson Gonzalez
[PHP-DEV] I this a bug in PHP 7.1.9?
September 04, 2017 02:30AM
I upgraded my PHP version on a server from 7.0 to latest 7.1.9 and got
this strange behaviour, did something changed on the core language or is
this a bug?

PHP sample code:
------------------------------------------
<?php

function test()
{
static $staticValue = test2();

return $staticValue;
}

function test2()
{
return array();
}

print_r(test());

------------------------------------------

Output:

PHP Fatal error: Constant expression contains invalid operations in
test.php on line 5

Fatal error: Constant expression contains invalid operations in test.php
on line 5

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
David Rodrigues
Re: [PHP-DEV] I this a bug in PHP 7.1.9?
September 04, 2017 03:00AM
From doc: http://php.net/manual/en/language.variables.scope.php

> Note: Static variables may be declared as seen in the examples above.
From PHP 5.6 you can assign values to these variables which are the result
of expressions, but you can't use any function here, what will cause a
parse error.

2017-09-03 21:27 GMT-03:00 Jefferson Gonzalez <[email protected]>:

> I upgraded my PHP version on a server from 7.0 to latest 7.1.9 and got
> this strange behaviour, did something changed on the core language or is
> this a bug?
>
> PHP sample code:
> ------------------------------------------
> <?php
>
> function test()
> {
> static $staticValue = test2();
>
> return $staticValue;
> }
>
> function test2()
> {
> return array();
> }
>
> print_r(test());
>
> ------------------------------------------
>
> Output:
>
> PHP Fatal error: Constant expression contains invalid operations in
> test.php on line 5
>
> Fatal error: Constant expression contains invalid operations in test.php
> on line 5
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
David Rodrigues
Jefferson Gonzalez
Re: [PHP-DEV] I this a bug in PHP 7.1.9?
September 04, 2017 06:50AM
On 09/03/2017 05:48 PM, David Rodrigues wrote:
> From doc: http://php.net/manual/en/language.variables.scope.php
>
>> Note: Static variables may be declared as seen in the examples above.
> From PHP 5.6 you can assign values to these variables which are the result
> of expressions, but you can't use any function here, what will cause a
> parse error.
Ahh, thanks a lot, I missed that one... I guess this change is to
optimize the performance of PHP somehow. Now I will have to make a
search on my code base to see where I'm using more functions that use
static variables to store the return value of a function (to speed stuff
up) and change it on a way that is compatible...

static $var = array();

if(empty($var))
{
$var = function_output();
}

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Rowan Collins
Re: [PHP-DEV] I this a bug in PHP 7.1.9?
September 04, 2017 03:10PM
On 4 September 2017 01:27:49 BST, Jefferson Gonzalez <[email protected]> wrote:
>I upgraded my PHP version on a server from 7.0 to latest 7.1.9 and got
>this strange behaviour, did something changed on the core language or
>is
>this a bug?
>
>PHP sample code:
>------------------------------------------
><?php
>
>function test()
>{
> static $staticValue = test2();
>
> return $staticValue;
>}
>
>function test2()
>{
> return array();
>}
>
>print_r(test());
>

According to 3v4l.org this has never been valid: https://3v4l.org/vRaNK The error message changed between 5.x and 7.x, but if you're already on 7.0 you shouldn't have seen any change.

Are you sure this test case correctly represents the code you're having problems with?

Regards,

--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login