title: How to check for a variable existence by a private function in a php Class?
body: <p>Here is a pseudo code of what I want to do:</p>
  
  <pre><code>class Test {
  
    protected  $myVar;
  
    public function __construct(){
        // get data from DB, returns null if not exist
        $this-&gt;myVar = getDataFromDatabase();
    }
  
    public function myFunc(){
  
         if(!$this-&gt;myVar){
             return response(['message' =&gt; 'data not found'],404);
         }
  
         // do other stuff
    }      
  }
  </code></pre>
  
  <p>This code is working well. Now I want to declare a private function to check for the existence of a variable (To clean my code and use the function everywhere).</p>
  
  <p>This is what I tried : </p>
  
  <pre><code>  public function myFunc(){
  
         $this-&gt;checkExistence();
  
         // do other stuff
    }  
  
    private function checkExistence(){
        if(!$this-&gt;myVar){
          return response(404);
        }
    }
  </code></pre>
  
  <p>But it is not working because I do not return the <code>$this-&gt;checkExistence();</code>! </p>
  
  <p>And when I try to return it: </p>
  
  <p><code>return $this-&gt;checkExistence();</code> </p>
  
  <p>It's working but the problem is when <code>$myVar</code> exists it is not hitting <code>// do other stuff</code> line because it is returning the whole function that makes sense! </p>
  
  <p>How should I transfer this existence checking to private function!?</p>