11

I'm running php behind nginx with php-fpm and cron tasks to php binary (/usr/bin/php).

I've found an inconsistency - the same script outputs different results when I run it through php binary and through fpm.

NOTE This only applies to PHP7. On another server I've tested it with 5.6 and the result is identical.

Here's what I've found. The following script:

<?php
class Test {
    public function test(){
        $arr = (object) [
            'children' => []
        ];
        $arr->children[] = 1;
        return $arr;
    }
}

$o = new Test();
$o->test();
print_r( $o->test() );

Saved to test.php. When I run it through browser (php-fpm), will produce:

stdClass Object
(
    [children] => Array
        (
            [0] => 1
        )
)

But when I execute it from CLI, the result is different:

[root@server1 web]# php -f test.php
stdClass Object
(
    [children] => Array
        (
            [0] => 1
            [1] => 1
        )
)

It does not happen without the (object) casting. Also if I'll instantiate $arr with new stdClass() it will not happen.

Seems like the $arr = (object) is being preserved in the memory by php7's engine.

Maybe it's a configuration issue. Anyone ran into it before or can explain?

Thanks.

galchen
  • 5,252
  • 3
  • 29
  • 43
  • There were some caching problems just before PHP7 was released, Possibly related to one of them. Was due to some speed optimisations – exussum Dec 08 '15 at 21:01
  • And doing `which php` points to `/usr/bin/php`, right? – Mr. Llama Dec 08 '15 at 21:04
  • 3
    This looks like a bug, I would suggest reporting it at http://bugs.php.net – Andrea Dec 08 '15 at 21:05
  • That's why you should wait for PHP 7.1 with production deployment :) – Marcin Orlowski Dec 08 '15 at 21:09
  • 1
    Could you please update with what PHP7 version you are running? – Oliver Nybroe Dec 08 '15 at 21:09
  • However @uruloke asks a good question. If you're using an RC version rather than the 7.0.0 release, this might be a bug which was already fixed. – Andrea Dec 08 '15 at 21:20
  • 2
    Yep, @Andrea, looks like it was introduced in rc2, exists in gold master, and 7.1. But... only affects classes. https://3v4l.org/PJR7m – bishop Dec 08 '15 at 21:20
  • Ah, so it *is* a new bug. – Andrea Dec 08 '15 at 21:22
  • @Mr.Llama that is correct (/usr/bin/php). uruloke I'm using 7.0.0 (not RC). MarcinOrlowski it's "semi-production", the server is used for local projects and not for the clients. You are absolutely right, I'll wait for upgrades before using it on our production servers. I'll report it – galchen Dec 08 '15 at 22:42
  • I'm encountering the same issue, please see https://stackoverflow.com/questions/62358900/different-output-of-exec-command-when-executed-in-browser-vs-terminal?noredirect=1#comment110287469_62358900 – Rav Jun 13 '20 at 11:25

1 Answers1

4

This was a bug in PHP 7 which had been reported and is now fixed:

https://bugs.php.net/bug.php?id=71067

SeinopSys
  • 8,787
  • 10
  • 62
  • 110
galchen
  • 5,252
  • 3
  • 29
  • 43