php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44542 __tostring does not work when using it for calling a function
Submitted: 2008-03-26 22:24 UTC Modified: 2008-07-17 23:13 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: account at dds dot nl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.5 OS: Windows Vista
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: account at dds dot nl
New email:
PHP Version: OS:

 

 [2008-03-26 22:24 UTC] account at dds dot nl
Description:
------------
When I try to use __tostring to call a function I get an error that 'the function name must be a string'. While calling a class member with __tostring does work.

--Steven

Reproduce code:
---------------
<?php
    class test {
        public $string = 'Class member named `string`.';

        public function __tostring() {
            return 'string';
        }

        public function string() {
            return "This is a string from a function.";
        }
    }
    $test = new test();
    $function_name = 'string';
    print $test."\n";
    print $test->$test."\n";
    print $test->$function_name()."\n";
    print $test->$test()."\n";
?>

Expected result:
----------------
---------- PHP ----------
string
Class member named `string`.
This is a string from a function.
This is a string from a function.

Output completed (0 sec consumed)

Actual result:
--------------
---------- PHP ----------
string
Class member named `string`.
This is a string from a function.

Fatal error: Method name must be a string in C:\projects\test.php on line 18

Output completed (0 sec consumed)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-30 16:02 UTC] php at xuefeng dot org
try this patch. 

Index: zend_vm_execute.h
===================================================================
RCS file: /repository/ZendEngine2/zend_vm_execute.h,v
retrieving revision 1.62.2.30.2.49.2.45
diff -u -r1.62.2.30.2.49.2.45 zend_vm_execute.h
--- zend_vm_execute.h	28 Mar 2008 14:34:59 -0000	1.62.2.30.2.49.2.45
+++ zend_vm_execute.h	30 Mar 2008 15:54:50 -0000
@@ -28885,6 +28885,7 @@
 {
 	zend_op *opline = EX(opline);
 	zval *function_name;
+	zval *name_is_object_method;
 	char *function_name_strval;
 	int function_name_strlen;
 
@@ -28893,6 +28894,13 @@
 
 	function_name = _get_zval_ptr_cv(&opline->op2, EX(Ts), BP_VAR_R TSRMLS_CC);
 
+	if (Z_TYPE_P(function_name) == IS_OBJECT && Z_OBJ_HT_P(function_name)->get_method != NULL) {
+		ALLOC_INIT_ZVAL(name_is_object_method);
+		if (zend_std_cast_object_tostring(function_name, name_is_object_method, IS_STRING TSRMLS_CC) == SUCCESS) {
+			function_name = name_is_object_method;
+		}
+	}
+
 	if (Z_TYPE_P(function_name)!=IS_STRING) {
 		zend_error_noreturn(E_ERROR, "Method name must be a string");
 	}
 [2008-04-01 21:38 UTC] felipe@php.net
This seems expected, as an object also isn't converted when passed on array key. (i.e. $arr[$obj]) and any other cases.
 [2008-07-17 23:13 UTC] jani@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


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 04:01:29 2024 UTC