#46409 [NEW]: __invoke method called outside of object context when using array_map

From: Date: Tue, 28 Oct 2008 20:17:51 +0000
Subject: #46409 [NEW]: __invoke method called outside of object context when using array_map
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-130235@lists.php.net to get a copy of this message
From:             phpbugs at sevenlight dot com
Operating system: Mac OSX 10.5.5
PHP version:      5.3.0alpha2
PHP Bug Type:     Scripting Engine problem
Bug description:  __invoke method called outside of object context when using array_map

Description:
------------
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:
---------------
<?php
class Callback
{
    protected $val = 'hello, world';
    
    public function __invoke()
    {
        return $this->val;
    }
}

$cb = new Callback();

// straight callback
if ($cb() != 'hello, world') {
    die('error');
}

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

Expected result:
----------------
Array
(
    [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


-- 
Edit bug report at http://bugs.php.net/?id=46409&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        http://bugs.php.net/fix.php?id=46409&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        http://bugs.php.net/fix.php?id=46409&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        http://bugs.php.net/fix.php?id=46409&r=trysnapshot60
Fixed in CVS:                        http://bugs.php.net/fix.php?id=46409&r=fixedcvs
Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=46409&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=46409&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=46409&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=46409&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=46409&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=46409&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=46409&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=46409&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=46409&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=46409&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=46409&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=46409&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=46409&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=46409&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=46409&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=46409&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=46409&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#130235) next »