php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28189 call_user_func, not in object context when called by class and func name
Submitted: 2004-04-27 20:37 UTC Modified: 2004-04-29 19:22 UTC
From: hrvinnie at yahoo dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.0.0RC2 OS: Debian Kernel 2.2.20-idepci
Private report: No CVE-ID: None
 [2004-04-27 20:37 UTC] hrvinnie at yahoo dot com
Description:
------------
PHP4 allowed one to call a function using call_user_func by passing in an array containing the class name and function name as strings.  

In PHP5RC1 and PHP5RC2, I have found the function is getting called.  However, using $this-> within the function triggers a fetal error.

My appologies, I was not sure what the best "Type" for this was, or if I'm crazy and doing something bone head wrong.

Reproduce code:
---------------
	class test_call_user_func

	{
		var $created = "false";
		
		function __construct()
		{
			$this->created = "true";
		}
		
		function isCreated()
		{
			$ret = $this->created;
			return $ret;
		}
		
	}

	$function = array('test_call_user_func', 'isCreated');
	print ("I have a return of isCreated [" . call_user_func($function) . "]"  );	
	

Expected result:
----------------
I have a return of isCreated [true]


Actual result:
--------------
Fatal error: Using $this when not in object context in /home/vfabello/workspace/loki/root/test3.php on line 16


Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-27 21:18 UTC] derick@php.net
This is not a bug, the error message here is totally correct as there is no instance of this class in your script, so you can't access $this here.
 [2004-04-27 23:14 UTC] hrvinnie at yahoo dot com
o then this behavior is new for PHP5?

I ask becuase while the online manual seems slient on invocation by class name, the examples, behavior of PHP4, and the 3rd Edition (for PHP5) of Core PHP by Zeev Suraski and Leon Atkinson seem to allow invocation by class name instead of instance.  

The books Listing 11.78 actually uses a call by class name.  There is no indication in the eratta this was not the case.
 [2004-04-27 23:15 UTC] hrvinnie at yahoo dot com
darned the errant cut and paste.  

The previous comment should have begun in a neutral questioning tone of "So, is this ..." ...
 [2004-04-27 23:47 UTC] amt@php.net
The behavior that's new in PHP 5 isn't the inability to 
call a method by call name instead of instance. It's 
that if you do so, PHP treats it as a static method 
call. When a static method is invoked in PHP 5, you 
cannot reference $this because it no longer exists and 
was never meaningful in the first place.
 [2004-04-29 19:22 UTC] hrvinnie at yahoo dot com
All, I appreciate your assistance and your contributions to PHP.

ahh!  So the behavior change is that instance functions are treated as static functions by call_user_func.   
 
IMHO, I believe that such behavior is fundamentally disconnected from the perpetrated behavior of accepted practices as set out by Zeev's book and other documentation.
 
However, this lets me move forward with my work.
 [2004-06-07 21:27 UTC] bug_php at dpits dot com
> This is not a bug, the error message here is totally correct as there is
> no instance of this class in your script, so you can't access $this
> here.

okay, but in PEAR (pear/Mail/RFC822.php) is following code:

class Mail_RFC822 {
/**
* A variable so that we can tell whether or not we're inside a
* Mail_RFC822 object.
* @var boolean $mailRFC822 */
var $mailRFC822 = true;
function parseAddressList($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
 {
                                                                                                            if (!isset($this->mailRFC822)) {
   $obj = new Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
   return $obj->parseAddressList();
  }
...

here an other script will use this classfunction with Mail_RFC822::parseAddressList($bla,..).


is this a php bug, because now we could not test whether or not we're inside a object?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 15:01:27 2024 UTC