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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stergosz
  • 5,754
  • 13
  • 62
  • 133

3 Answers3

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.

Community
  • 1
  • 1
George Reith
  • 13,132
  • 18
  • 79
  • 148
  • 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](http://stackoverflow.com/questions/1147937/php-curly-brace-syntax-for-member-variable) which gives the wrong link, too. -- [PHP Variable Names: Curly Brace Madness](http://cowburn.info/2008/01/12/php-vars-curly-braces/) – hakre Aug 30 '12 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. – George Reith Aug 30 '12 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;
hakre
  • 193,403
  • 52
  • 435
  • 836
Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
  • 1
    The square brackets around `$mehtod` are superfluous and are not needed: http://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 Aug 30 '12 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. – Berry Langerak Aug 30 '12 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: http://php.net/manual/en/functions.variable-functions.php – hakre Aug 30 '12 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. – Berry Langerak Aug 30 '12 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 Aug 30 '12 at 10:49
0

If you need to call a function with a dynamic name, have a look at http://de2.php.net/manual/en/function.call-user-func-array.php or http://de2.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: http://de2.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.

steffen
  • 16,138
  • 4
  • 42
  • 81