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

why i get this error? how fix it? when run on server get Fatal error: Method name must be a string line 8

class Model extends Core_Model_Config_Data
{
    protected function Load()
    {
        $a = file_properties();  
        $x0 = $this->$a["x0"]();       line 8 error
        $x0 = $this->$a["x1"]($x0);
        $this->$a["x2"]($x0);
    }
}

please help me.

share|improve this question

closed as off-topic by Matteo Tassinari, Hanky 웃 Panky, Jan Dvorak, FractalizeR, Panique Oct 27 '13 at 16:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." – Matteo Tassinari, Jan Dvorak, FractalizeR, Panique
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
So what $x0 = $this->$a["x0"](); is supposed to be? –  Hanky 웃 Panky Oct 27 '13 at 14:15
    
what is the result of var_dump($this->$a["x0"]) ?? –  zzlalani Oct 27 '13 at 14:16
    
@Hanky웃Panky yeah you are right –  zzlalani Oct 27 '13 at 14:24
add comment

2 Answers

Try this:

protected function Load()
{
    $a = file_properties(); 

    $f = $a['x0'];
    $x0 = $this->$f();

    $f = $a['x1'];
    $x0 = $this->$f($x0);

    $f = $a['x2'];
    $this->$f($x0);
}

Obviously the values $a['x0'], $a['x1'], $a['x2'] must be strings and hold a valid method name for the class.

share|improve this answer
    
isn't fixed error in $x0 = $this->$f(); my php version is 5.2.17 –  user699998 Oct 27 '13 at 16:27
    
Please read my answer better: Obviously the values $a['x0'], $a['x1'], $a['x2'] must be strings and hold a valid method name for the class. what are they're actual values? –  Matteo Tassinari Oct 28 '13 at 6:36
add comment

Right, there's a variety of possible reasons for your code throwing up errors:

  • What is $a["x0"]? is it a string? If so, does the method even exist?
  • Ambiguity: The preprocessor might have a hard time working out what you're trying to do, should the string value of $a be used to reference a property, that might be an array, that has a key "x0", which in turn might be a Closure instance, or a string that is a method name? use cruly braces to be clear: $this->{$a["x0"]}();
  • At no point are you cheking if the method exists, let alone if it can be called... where are your method_exists($this, $a["x0"]) and is_callable(array($this, $a["x0"])) checks?
  • Your code is flawed from the off, it's so incredibly error-prone... I wouldn't even bother working this one out. I'd set about rethinking my approach to whatever problem you're trying to solve here, and start over.
share|improve this answer
add comment

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