title: PHP equivalent of Python's __name__ == "__main__"?
body: <p>As per the title, is there PHP equivalent of <code>__name__ == "__main__"</code>?</p>
  
  <p>Is there something that would work for both scripts executed through the command line and through a web request, or would a custom function be needed?</p>
  
  <p>For those unfamiliar with Python, <code>__name__ == "__main__"</code> allows you to define a module file, and also have some things that allow you to run it if it is the entry point. The equivalent structure in PHP would resemble this:</p>
  
  <pre><code>// SomeClass.php
  &lt;?php
  class SomeClass
  {
      function doStuff() {
          echo "wahey!\n";
      }
  }
  
  // python, I know.
  if (__name__ == "__main__") {
      $sc = new SomeClass;
      $sc-&gt;doStuff();
  }
  ?&gt;
  
  // OtherClass.php
  &lt;?php
  require_once("SomeClass.php");
  class OtherClass
  {
      public $yep;
  }
  ?&gt;
  
  // command line:
  php SomeClass.php     // outputs "wahey!"
  php OtherClass.php    // outputs nothing
  </code></pre>
  
  <hr>
  
  <p><strong>Note</strong>:
  <a href="https://stackoverflow.com/a/2414026/42223">zerkms' answer</a> is the best, but is not quite right - it should read:</p>
  
  <pre><code>if (!debug_backtrace()) {
      // do useful stuff
  }
  </code></pre>
  
  <p>This is significantly faster than !count(debug_backtrace()), which itself is about twice as fast as my solution involving realpath().</p>