Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
 
What version of PHP are you using? The above code, after fleshing out (example code should be self-contained) doesn't produce the error you have in your title when run under PHP 5.3.2. It's also best to include the error and an indication of which line generates it within the body of a post, so that there's no confusion over what is happening. –  outis Nov 14 '10 at 10:38
 
I am testing the code on a live site--somewhere in the subfolders. The site is hosted on hostgator I think... –  Joann Nov 14 '10 at 13:32
 
That doesn't answer my question. If you're not certain which PHP version your host runs, you can use phpversion to find out. In order to fix a problem with code, the problem needs to be reproducible. Right now, it's not. Some crucial information is missing from your question. Also, the second sample (using brackets) loops over $index but uses $key in the loop body. If that's representative of your actual code, you're referring to an undefined variable ($key), which will cause a warning. –  outis Nov 15 '10 at 5:12
 
I am using version 5.3.1. No, $key and $index are the same, sorry for the inconsistencies. Thanks for your help! –  Joann Nov 16 '10 at 15:34
add comment

3 Answers

up vote 1 down vote accepted

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).

share|improve this answer
 
This works pefectly, thanks! Question: What does this line $query[] = "$key={$_POST[$key]}"; do? Particulary the one in double quotes. It will result to something like this $query = array("key" => "value"); right? –  Joann Nov 16 '10 at 15:26
 
No. It creates a string, not an array. The curly brackets are for complex syntax. The empty square brackets are for auto-indexing. To store a key and value in an array, I'd use $query[$key] = $value, not $query = array($key, $value), except in very particular circumstances. –  outis Nov 17 '10 at 4:38
add comment

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]])){
share|improve this answer
add comment

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

http://de2.php.net/manual/en/function.call-user-func.php

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.