Joann
Joann

Reputation: 1377

PHP error: method name must be a string

I have found a bug report(http://bugs.php.net/bug.php?id=24286) about this though I am not sure if it's relevant to my problem and it's very old. This is my code:

class RegExp {
    function name($e){ .... }
    function email($e){ .... }
    function phone($e){ .... }
}    

$regexp = new RegExp();
$final_keys= array("name", "email", "phone");

for($index=0; $index < count($final_keys); $index ++){
    if(!$regexp->$final_keys[$index]($_POST[$final_keys[$index]])){
        $invalid_uri_vars .= $final_keys[$index].",";
    }
    else{
        $uri_vars = "&".$final_keys[$index]."=".$_POST[$final_keys[$index]];
    }
}

What it does is it uses a value from an array as the name of the method to be called. In other words, I am trying to implement a function call using a variable $final_keys[$index] as its name.

*UPDATE: I tried implementing the suggestions below, nothing seems to work. Here's one of my modifications as suggested:

for($key=0; $key < count($final_keys); $key ++){
    if(!$regexp->{$final_keys[$key]}($_POST[$final_keys[$key]])){
        $invalid_uri_vars .= $final_keys[$key].",";
    }
    else{
        $uri_vars = "&".$final_keys[$key]."=".$_POST[$final_keys[$key]];
    }
}

I get the same error as my original code. Another method using call_user_func but I am not sure if did it right:

for($key=0; $key < count($final_keys); $key++){
    if(!call_user_func(array($regexp, $final_keys[$key]), $_POST[$final_keys[$key]])){
        $invalid_uri_vars .= $final_keys[$key].",";
    }
    else{
        $uri_vars = "&".$final_keys[$key]."=".$_POST[$final_keys[$key]];
    }   
}

And I get this error: Warning: call_user_func(Array) [function.call-user-func]: First argument is expected to be a valid callback in /.........testreqmeta.php on line 91

Upvotes: 0

Views: 1999

Answers (3)

outis
outis

Reputation: 77400

I'm still not getting any errors (other than the undefined $key in the second sample) when trying the sample code, so I can't say for certain what will fix it, but here's an approach that simplifies things and gets rid of the array indexing operation: use a foreach loop rather than a for loop.

$query = array();
foreach ($final_keys as $key) {
    if(!$regexp->$key($_POST[$key])) {
        $invalid_uri_vars[] = $key;
    } else {
        $query[] = "$key={$_POST[$key]}";
    }
}
$uri_vars = implode('&', $query);

I've also replaced the repeated string appends with an array implosion, which should be slightly more performant. It also makes it easier to further process the query string, if necessary, before submitting it. There are better approaches yet, but that's a whole other topic.

NB. RegExp isn't a very good description of what the class does, hence it isn't a very good name. The class may use regular expressions, but it isn't itself a regular expression (which support operations such as "match" and "replace"; RegExp's API has none of these).

Upvotes: 1

aefxx
aefxx

Reputation: 25249

There's a function for what you are trying to achieve call_user_func:

https://www.php.net/manual/en/function.call-user-func.php

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798804

Quoting from your link:

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');
?>

So then:

if($regexp->{$final_keys[$index]}($_POST[$final_keys[$index]])){

Upvotes: 1

Related Questions