-1

Code:

public function isQuestion($query){

    $questions = $this->getAllQuestions();

    if (count($questions)){
            foreach ($questions as $q){
                if ($this->isQuestion$q($query)){
                    return $this->isQuestion$q($query);
                }
            }
        }

    return false;
}

Error:

Parse error: syntax error, unexpected T_VARIABLE in /Applications/XAMPP/xamppfiles/htdocs/ai/application/models/question_model.php on line 7

The problem occurs in:

if ($this->isQuestion$q($query)){

return $this->isQuestion$q($query);

I have some functions like isQuestion1, isQuestion2, isQuestion3, etc... and I call another function getAllQuestions that will return me all the numbers of the questions in an array like 1,2,3,4,5....

Then I use the above code to check if each function is a question based on a query.

1
  • 1
    Wrap it in curly brackets $this->isQuestion{$q}($query) Commented Aug 30, 2012 at 9:51

3 Answers 3

2

The problem is with your method isQuestion$q.

The $ denotes the start of a variable and is confusing the interpreter.

Write it like so:

isQuestion{$q}

The curly braces allow you to insert a variable into a string (or anything with string representation). Read Curly braces in string in PHP for more information.

2
  • The link is about the wrong thing. This is not about double-quoted strings. Unfortunately, this type of usage of the curly brackets (outside of a double-quoted string) is more or less undocumented in the PHP manual. Compare PHP curly brace syntax for member variable which gives the wrong link, too. -- PHP Variable Names: Curly Brace Madness
    – hakre
    Commented Aug 30, 2012 at 10:00
  • @hakra Indeed, it is supplementary information because I'm assuming the OP hasn't encountered curly braces yet. I too couldn't find anything in the PHP docs. Commented Aug 30, 2012 at 10:05
1

Well, the following is invalid syntax:

if ($this->isQuestion$q($query)){

Try this instead:

foreach ($questions as $q) {
    if ($result = $this->{'isQuestion' . $q}()) {
        return $result;
    }
}
return false;
5
  • 1
    The square brackets around $mehtod are superfluous and are not needed: codepad.org/joagmyXQ - Also an array with foreach you do not need to check count first if you just return early on success. I edited the question for that.
    – hakre
    Commented Aug 30, 2012 at 10:16
  • @hakra I agree on the if( $result = blah( ) ) { return $result; } Still, the square brackets I would keep: it becomes much more obvious that you're calling a dynamically named function, so I've added it for clarity. Nevertheless, your improvement seems solid. Commented Aug 30, 2012 at 10:25
  • Well variable functions are common in PHP - and documented. But they need to start with a $. Or you use the square brackets like in the edit now. Variable functions are here just for reference: php.net/manual/en/functions.variable-functions.php
    – hakre
    Commented Aug 30, 2012 at 10:27
  • @hakra I'm fully aware of that, but it's $this->{$method}() vs $this->$method(). Both are equally valid, but I think the first makes it more obvious that a dynamic function is being called. Future me always thanks me. In the end, it's just a matter of style though. Commented Aug 30, 2012 at 10:47
  • Yeah and you should keep away stuff you do not need. E.g. for the full dynamic character you could do stuff like $this->{"$method"}() (string usage is known, right?! ;) ) and even more. Not a good idea IMHO. But style questions need time to resolve, so I hope to see you in the less is more camp some day ;)
    – hakre
    Commented Aug 30, 2012 at 10:49
0

If you need to call a function with a dynamic name, have a look at https://www.php.net/manual/en/function.call-user-func-array.php or https://www.php.net/manual/en/function.call-user-func.php

You might want to ensure that the method really exists in order to avoid getting fatal errors: https://www.php.net/manual/en/function.method-exists.php

Also check if you want to replace

if ($this->isQuestion$q($query)){
    return $this->isQuestion$q($query);
}

with

if ($this->isQuestion$q($query)){
    return true;
}

In general it might be better to create an interface Question and hold an array with the Question instances to be asked.

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