php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34690 call_user_func and scope resolution operator ( :: )
Submitted: 2005-09-30 15:19 UTC Modified: 2005-09-30 16:28 UTC
From: claudio dot frizziero at nereal dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.5 OS: Linux (Fedora Core 4)
Private report: No CVE-ID: None
 [2005-09-30 15:19 UTC] claudio dot frizziero at nereal dot com
Description:
------------
When I use call_user_func (and call_user_func_array, of course) inside a method to call a method of another class I'm expecting it works like the scope resolution operator.

If it is not a bug: is it possible to add a flag, or alternatively add two similar function (i.e. call_user_func_Paamayim_Nekudotayim and call_user_func_array_Paamayim_Nekudotayim 8), to avoid the eval workaround?

Thank you!


Note: i'm using php 5.0.4, but I don't find any fix in the changelog to 5.0.5

Reproduce code:
---------------
class MyClass {
   public $string = 'Hello!';

   public function SayHello() {
      call_user_func(array('OutputClass', 'WriteToOutput'));
      //
   }

   public function SayHello2() {
      OutputClass::WriteToOutput();
   }

   public function SayHello3($outputclass, $outputmethod) {
      // $outputclass::$outputmethod(); // syntax not allowed, with or without curly braces
      eval($outputclass . '::' . $outputmethod . '();');  // it works, but it's a workaround
   }
}

class OutputClass {
   public function WriteToOutput() {
      echo ($this->string);
   }
}

$a = new MyClass();
$a->SayHello();   // Fatal error: Using $this when not in object context
$a->SayHello2();  // it works (wow!)
$a->SayHello3('OutputClass', 'WriteToOutput');  // it works only using eval


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-30 15:22 UTC] tony2001@php.net
Long version:
Please be more verbose and try to describe the problem in more details. Fields in the report form like "expected result" and "actual result" are given for you to fill them, not to ignore.
Short version: 
I don't get the problem.
 [2005-09-30 15:41 UTC] claudio dot frizziero at nereal dot com
Expected result:

to see "Hello!" on my browser (or console) when I call $a->SayHello();

Actual result:

Fatal error: Using $this when not in object


You don't get the problem? What do you get? 3 "Hello!"?
 [2005-09-30 15:46 UTC] tony2001@php.net
There is no $this when you call a static method.
The error message clearly says that.
 [2005-09-30 16:28 UTC] claudio dot frizziero at nereal dot com
I wrote:

"If it is not a bug: is it possible to add a flag, or alternatively add two similar function [...], to avoid the eval workaround?"

My question is not if it is a bug or not a bug, my question is to resolve the problem. The problem is that I MUST use the "eval" - a dirty workaround - because:
- php parser doesn't accept :: operator and 2 vars (but it doesn't matters, because I can't pass a dynamic number of arguments)
- call_user_func (and call_user_func_array) calls the function in a way that doesn't have an equivalent in the PHP syntax (both $class->method() and $class::method() work in two different way - the first will see $this of its class, the second will see $this of the class where it is called)

If i'm not explained, tell me and I'll make some examples...
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 06:01:38 2025 UTC