php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24286 using array from class as function name
Submitted: 2003-06-22 10:54 UTC Modified: 2003-11-29 01:37 UTC
Votes:4
Avg. Score:4.5 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: terjeto at stud dot ntnu dot no Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2003-06-22 (dev) OS: RedHat 9.0
Private report: No CVE-ID: None
 [2003-06-22 10:54 UTC] terjeto at stud dot ntnu dot no
Description:
------------
When using a string from an array inside an class for 
calling a function it results in a fatal error.

Reproduce code:
---------------
<?php

class Foo {
	var $_array = array( 'function' => array ( 'spacer' => array( 0 => '_funk' ) ) );
}

function _funk( $arg ) {
	print( $arg );
}

$_out = array( 'function' => array ( 'spacer' => array( 0 => '_funk' ) ) );

_funk( "Test string 1<br>" );

$foo = new Foo();

$foofunk = $foo->_array['function']['spacer'][0];

$foofunk( "Test string 2<br>" );

$_out['function']['spacer'][0]( "Test string 3<br>" );
$foo->_array['function']['spacer'][0]( "Test string 4<br>" );
?>

Expected result:
----------------
Test string 1
Test string 2
Test string 3
Test string 4

Actual result:
--------------
Test string 1
Test string 2
Test string 3

 Fatal error:  Method name must be a string in /var/
www/html/test.php on line 22

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-29 10:55 UTC] iliaa@php.net
Works fine in php 4.3.3, so this appears to be a BC issue.
 [2003-07-09 08:09 UTC] bloso at mailbr dot com dot br
I've got the same error message:

Fatal error: Method name must be a string in /var/www/htdocs/scorphus/phplab/countracker/tpl/templates_c/%%200/%%2001463753/index.html.php on line 28

index.html.php line 28:
<br><?php echo $this->_plugins['function']['eval'][0](array('var' => $this->_config[0]['vars']['credits']), $this) ; ?>

PHP Version: 5.0.0b2-dev (php5-200307071930.tar.gz)
Configure Command: './configure' '--with-mysql' '--with-apxs'
System: Slackware 9 (2.4.20) and Debian 3.0 r1 (2.4.20)
Also unsing: Smarty 2.4.2

Hope it helps,
Pablo.
 [2003-07-10 06:25 UTC] tony2001 at phpclub dot net
There is no matter what Smarty version do you use.
PHP complains on this syntax:
<?
class Test {
    var $array_var = Array();
    function test() {
        $this->array_var[0][0][0] = "test_method";
    }
    function test_method($echo_var) {
        echo $echo_var;
    }
}

$test_obj = new test;
$test_obj->array_var[0][0][0]("simple test"); //line 15
?>
Fatal error: Method name must be a string in /www/index.php on line 15

Guys, please, fix this very annoying bug at least in beta2.
On 10 Jul 2003 this bug still exists in CVS.
 [2003-07-10 07:19 UTC] stas@php.net
What is expected to be result of $test_obj->array_var[0][0][0]("simple test") call?

Which function or method of which object should be called? What with $test_obj->foo->array_var[0][0][0]("simple test") - should the calling object be $test_obj and function name array_var[0][0][0] (should it be global $array_var[0][0][0]? global $foo->array_var[0][0][0]? or property foo->array_var[0][0][0] of $test_obj?) or the calling object should be $test_obj->foo and array_var[0][0][0] should be the method name?
 [2003-07-10 07:57 UTC] tony2001 at phpclub dot net
I expect that using attribute of the object as method name it will work as it's already working with PHP4.

That is:
$object->attribute = 'method_name';
and with
$object->attribute();
$object->method_name(); should be called.
Maybe I'm wrong, but in case of $object->obj_attribute->attribute(); we have the same situation - we're calling $object->obj_attribite->{method, which name contains $object->obj_attribute->attribute }();

Am I wrong?
 [2003-07-27 07:13 UTC] stas@php.net
Why with "$object->attribute()"
"$object->method_name()" should be called and not "$object->attribute()" and how the engine is supposed to tell that? Is $foo->x() now meaning "call method 'x' of object '$foo'" or "take property 'x' of object '$foo' and call method with this name on object '$foo'"? My opinion is that it _always_ should mean the latter and never the former. 
 [2003-07-29 15:38 UTC] tony2001 at phpclub dot net
Maybe you're right. But this syntax works as I expect with PHP4 and I suppose this is a BC problem.
I really do not know how implementation of this syntax in ZE2 differs from ZE1, but I expected it to continue to work in the same way. My apologies if I was wrong.
 [2003-07-31 03:21 UTC] tony2001 at phpclub dot net
ok, here is some workaround on this problem:
in case of
<?
class Test {
    var $var = Array('test_function');
    function test_function($echo_var) {
        echo $echo_var;
    }
}

$test_obj = new test;
$test_obj->var[0]('bla');
?>
you can avoid Fatal error in last string using this instead:
<?
$test_obj->{$test_obj->var[0]}('bla');
?>

But in case of:
<?
class Test {
    var $var = Array('test_function');
}

function test_function($echo_var) {
    echo $echo_var;
}
?>
(i.e. you need to call a function, not a method, which name is contained in objects, attribute) it seems, that there is no way, but use this temporary variable:
<?
$test_obj = new test;
$tmp = $test_obj->var[0];
$tmp('bla');
?>

And another one important info:
in CVS version of Smarty this trouble is already solved.
 [2003-11-29 01:37 UTC] sniper@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

Same as bug #25652 (shorter report + good example)


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Aug 19 02:01:28 2024 UTC