#40061 [Bgs]: Repeated dereferencing in object callbacks
| From: | a at b dot c dot de | Date: | Tue, 09 Jan 2007 04:21:29 +0000 |
| Subject: | #40061 [Bgs]: Repeated dereferencing in object callbacks | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-107345@lists.php.net to get a copy of this message | ||
ID: 40061
User updated by: a at b dot c dot de
Reported By: a at b dot c dot de
Status: Bogus
Bug Type: Feature/Change Request
Operating System: Windows XP SP2
PHP Version: 5.2.0
New Comment:
If my head had been together I would have checked what I was saying
about PHP 4 and I would CERTAINLY have tried array($a->b, 'foo') as
being obvious: I could have sworn I had and this just proves that my
head was not as together as I thought it was at the time. Sorry for
wasting YOUR time because of it.
Previous Comments:
------------------------------------------------------------------------
[2007-01-08 11:53:39] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
$foo->bar->baz() was possible in PHP 4, too. New in PHP 5 was
$foo->bar()->baz() (with "bar" being a function)
To your Problem: Just use array($a->b,'foo') as callback.
------------------------------------------------------------------------
[2007-01-08 11:48:19] a at b dot c dot de
Description:
------------
Object callbacks $foo->bar() are represented as arrays array($foo,
'bar'). Since PHP now supports the syntax $foo->bar->baz(), it seems at
first sight that callback arrays could be nested similarly.
Currently to use the baz() method above in a callback, one must use a
temporary variable $temp=$foo->bar, and use array($temp, 'baz') as the
callback in the same way that in PHP4 it was necessary to write
$temp=$foo->bar; $temp->baz().
It would seem syntactically unambiguous to write the callback as
array(array($foo, 'bar'), 'baz'). There is the matter that its first
element refers to an object, not a method, but that's what the first
element of an array callback is supposed to refer to (static class
methods notwithstanding).
Reproduce code:
---------------
<?php
class aclass
{
public $b;
}
class bclass
{
public function foo($v)
{
return $v*2;
}
}
$t =
array('1','2','3','4','5','6','7','8','9');
$a = new aclass;
$a->b = new bclass;
print_r(array_map(array(array($a, 'b') ,'foo'), $t));
Expected result:
----------------
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
[5] => 12
[6] => 14
[7] => 16
[8] => 18
)
Actual result:
--------------
Warning: array_map(): The first argument, 'Array', should be either
NULL or a valid callback in ...
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=40061&edit=1