0

I have the problem that my data variable is suddenly null. It's happening directly after an IF-Statement when nothing was written to this variable. Does anyone knows what it is happening here?

public function render()
    {
        ob_start();
        if($this->ajax)
            $ext = '.ajax';
        else if(file_exists($this->scriptPath.$this->template.'.mst'))
            $ext = '.mst';
        else
            $ext = '.phtml';
        #var_dump($this->data); // <-- is filled with many data
        if($ext === '.mst'){
            var_dump($this->data); // <-- is null
            $mustache = new \Mustache_Engine(
                array(
                    'escape' => function($value){return $value;},
                    'partials_loader' => new \Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/../../../frontendTarget/classes/lib/de/preis/frontend/viewFragments/partials',array('extension' => 'mst'))
                )
            );
            $content = file_get_contents($this->scriptPath.$this->template.$ext);
            $content = $mustache->render(($content),$this->data);
            echo $content;
        } else {
            include $this->scriptPath.$this->template.$ext;
        }
        return ob_get_clean();
    }

I've here two var_dumps(). One before the if, where the var is filled with data and one after the if, where the data is suddenly completely gone.

Could anyone assist me on this one? Thanks in advance

4
  • When var_dump($this->data); return data, what is the value of $ext? Maybe when the value is '.mst' there is no data Commented Aug 23, 2017 at 11:19
  • 1
    if you un-comment the first var_dump() so both of them dump their data, are you getting the first with data and the second displays null? or are you running the code twice commenting each var_dump() in/out so you only execute 1 of them? Commented Aug 23, 2017 at 11:21
  • Possible duplicate? Variable becomes NULL in IF statement Commented Aug 23, 2017 at 11:28
  • @Nerea the value is '.mst', otherwise the 2nd var_dump would not run at all. @WeeZel I did not mention it, but I checked the solutions of the "duplicate question" and the solutions from similar questions. They did not help. I run the code twice with one var_dump in and one out. I just ran it again with both in and realized that I get the output of the first one with data and a 2nd output of the first one where it is empty. Maybe you got a good point here. I will follow up with this new information. Commented Aug 23, 2017 at 11:37

2 Answers 2

1

More of a learned lesson than an answer!
I had a problem like this once that seemed like a real mystery, it took me while to figure out because it didn't make any sense at the time. My answer was that I was running my code with netbeans debugger and I had previously set a watch to clear (unset()) a variable to allow me to debug a code segment - I'd forgotten to remove the watch so it was executing during my debug session and nulling my variable

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to @Wee Zel for the hints. I double checked my code and figured out that the class method was called twice. In the first case, the data were given, in the 2nd it was not. In my particular case, I had a MVC model where the layout was rendered first (with data) and then the view (without data). I needed the data in the view and not the layout. So I just had to pass the data to the correct layer.

1 Comment

glad you got to the bottom of it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.