php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40061 Repeated dereferencing in object callbacks
Submitted: 2007-01-08 11:48 UTC Modified: 2007-01-09 04:21 UTC
From: a at b dot c dot de Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.0 OS: Windows XP SP2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: a at b dot c dot de
New email:
PHP Version: OS:

 

 [2007-01-08 11:48 UTC] 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 ...


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-08 11:53 UTC] 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-09 04:21 UTC] a at b dot c dot de
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 18 18:00:02 2025 UTC