php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51998 call_user_func_array can't use private methods on the $this pointer
Submitted: 2010-06-05 09:05 UTC Modified: 2010-06-05 10:38 UTC
From: php at paulisageek dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.13 OS: All
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at paulisageek dot com
New email:
PHP Version: OS:

 

 [2010-06-05 09:05 UTC] php at paulisageek dot com
Description:
------------
I'd like to make a helper library that can use call_user_func outside of the 
object context, but still use private methods (obviously only when called from 
inside the object).

Example attached:

Test script:
---------------
<?php

class A { 
  private function test1() {
    print 'side effect 1';
    return false;
  }
  private function test2() {
    print 'side effect 2';
    return 'yay';
  }
  private function test3() {
    print 'side effect 3';
    return 'nope';
  }
  public function first() {
    return nonempty_lazy(
      array(array($this, 'test1')),
      array(array($this, 'test2')),
      array(array($this, 'test3')));
  }
}

function nonempty_lazy() {
  $args = func_get_args();
  foreach ($args as $arg) {
    $cb = array_shift($arg);
    $val = call_user_func_array($cb, $arg);
    if (!empty($val))
      return $val;
  }
}

$a = new A;
print $a->first();


Expected result:
----------------
side effect 1side effect 2yay

Actual result:
--------------
Warning: call_user_func_array() expects parameter 1 to be a valid callback, cannot 
access private method A::test1() in /Users/ptarjan/tmp/cufa.php on line 28

Warning: call_user_func_array() expects parameter 1 to be a valid callback, cannot 
access private method A::test2() in /Users/ptarjan/tmp/cufa.php on line 28

Warning: call_user_func_array() expects parameter 1 to be a valid callback, cannot 
access private method A::test3() in /Users/ptarjan/tmp/cufa.php on line 28


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-05 10:32 UTC] degeberg@php.net
-Status: Open +Status: Bogus
 [2010-06-05 10:32 UTC] degeberg@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Of course you cannot call private instance methods outside of the object. That's the entire point of private methods.
 [2010-06-05 10:38 UTC] php at paulisageek dot com
But the originating request came from inside the object. With first class 
functions, you could just pass those around once you were inside the object (think 
python or javascipt). We should emulate the same in PHP.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Dec 05 19:00:01 2025 UTC