title: How to exit from a specific function in PHP?
body: <p>I have multiple nested methods inside a PHP class. What I want to do is, based on certain circumstances, I want to exit from NOT JUST the current method, but 2 above it, then the leftover code should continue running. Now the issue with die(), exit() is that they end the full script and I don't want that. I simply want to go a few methods up and continue the script.</p>
  
  <p>Of course, there's the old school method of returning a value in each method and check if it's false for example. But that way I'll have to write tons of additional code if I have like 50 nested methods. Here's what I have right now - it's a very basic usage here, I'm using it in a lot more complicated scenarios (using PHP 7.2.4):</p>
  
  <pre><code>class Sites
  {
      public function __construct()
      {
          $this-&gt;fn1();
      }
  
      public function fn1()
      {   
          $fn2 = $this-&gt;fn2();
  
          echo 'I want this to be displayed no matter what!';
      }
  
      public function fn2()
      {       
          $fn3 = $this-&gt;fn3();
  
          if ($fn3)
          {
              return true;
          }
      }
  
      public function fn3()
      {       
          $fn4 = $this-&gt;fn4();
  
          if ($fn4)
          {
              return true;
          }
      }
  
      public function fn4()
      {       
          $random = rand(1, 100);
  
          if ($random &gt; 50)
          {
              return true;
          }
          else
          {
              // I want to exit/break the scirpt to continue running after
              // the $fn2 = $this-&gt;fn2() call in the $this-&gt;fn1() function.
              exit();
  
              echo "This shouldn't be displayed.";
          }
      }
  }
  </code></pre>
  
  <p>Just as mentioned in the code comments, I want to break the script - if the random number is below 50 and go back to <code>fn1()</code> but continue executing the <code>echo</code> function there.</p>
  
  <p>Is this possible somehow? Please let me know if you need more information and I'll provide.</p>