|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46409 __invoke method called outside of object context when using array_map
Submitted: 2008-10-28 20:17 UTC Modified: 2008-11-28 09:42 UTC
From: phpbugs at sevenlight dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3CVS-2008-11-11 OS: *
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:
Bug Type:
From: phpbugs at sevenlight dot com
New email:
PHP Version: OS:


 [2008-10-28 20:17 UTC] phpbugs at sevenlight dot com
When using a custom class as a callback object, the array_map (and possibly other methods) call the method outside of the object's context.

I was using this as the __invoke() method to test:

        if (!isset($this)) {
            die('Outside of object context.');
        } else {
            return $this->val;

This may be the best way to setup the PHP unit test script.

Again I did not test this on other callback functions, but it very easily could be happening on those as well.

Reproduce code:
class Callback
    protected $val = 'hello, world';
    public function __invoke()
        return $this->val;

$cb = new Callback();

// straight callback
if ($cb() != 'hello, world') {

// array_map
$a = array(1, 2, 3);
$b = array_map($cb, $a);

Expected result:
    [0] => hello, world
    [1] => hello, world
    [2] => hello, world

Actual result:
PHP Fatal error:  Using $this when not in object context in /srv/www/test/closure.php on line 8


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-28 09:42 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Mar 07 03:01:23 2021 UTC