php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #59122 Return values that are class private variables are null'd
Submitted: 2010-03-16 01:29 UTC Modified: 2011-05-17 09:09 UTC
From: brian at moonspot dot net Assigned: hradtke (profile)
Status: Closed Package: gearman (PECL)
PHP Version: 5.2.12 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: brian at moonspot dot net
New email:
PHP Version: OS:

 

 [2010-03-16 01:29 UTC] brian at moonspot dot net
Description:
------------
If you use a class method as the function for doing work and you return a private variable of the class as the result, the private variable is set to null.

Reproduce code:
---------------
<?php

class Foo {
    private $count = 0;
    public function run($job) {
        $this->count++;
        return $this->count;
    }
}

$foo = new Foo();

$w = new GearmanWorker();

$w->addServer("127.0.0.1");

$w->addFunction("foo", array($foo, "run"));

while($w->work());

?>

Expected result:
----------------
On each run of this function, the result should increase by 1.

Actual result:
--------------
The result of this function is always 1.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-17 14:56 UTC] Jared dot Williams at ntworld dot com
Adding an explict cast to string on the return fixes it..

return (string)$this->count;
 [2010-04-18 11:49 UTC] brian at moonspot dot net
Yes, that forces a copy. That is the workaround. But, clearly, this is not the right solution.
 [2010-04-18 13:46 UTC] Jared dot Williams at ntlworld dot com
Yeah, even

$w = new GearmanWorker();
$w->addServer("127.0.0.1");
$count = '3';
$w->addFunction("foo", function($job) use ($count) { return 
$count; });
while($w->work());

--

$c = new GearmanClient();
$c->addServer("127.0.0.1");
var_dump($c->do('foo', ''));                             
var_dump($c->do('foo', ''));                            

Outputs

string(1) "3"
string(0) ""

Seems the return zval is getting freed/destroyed when it 
should be.
 [2010-04-19 08:24 UTC] brian at moonspot dot net
In PHP 5.2 it does not destroy a global in a normal function or a static. I sat down with James Luedke last week and showed him exactly what was going on. It is going to trace it down to see why his code thinks it is ok to free for classes and I guess this use case as well.
 [2011-05-17 09:09 UTC] hradtke@php.net
This bug has been fixed in SVN.

In case this was a documentation problem, the fix will show up at the
end of next Sunday (CET) on pecl.php.net.

In case this was a pecl.php.net website problem, the change will show
up on the website in short time.
 
Thank you for the report, and for helping us make PECL better.


 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Jan 19 07:01:23 2020 UTC