Why isnt "new" throwing error if class is not declared in PHP?
<p>Lets have two files: index.php and foo.php
The foo.php file would be placed in <strong>subdirectory /lib</strong></p>

<p>now in index.php, lets have this function</p>

<pre><code>function bar(){
  require('foo.php');
  $foo = new Foo();
  $foo-&gt;bar();
}
</code></pre>

<p>And in foo.php</p>

<pre><code>class Foo{
  public function __construct(){
      echo "baz";
  }

  public function bar(){
     echo "bar";
  }

}
</code></pre>

<p>Now, the error is hard to spot: I made error in requiring the class Foo, because instead <code>require('lib/foo.php')</code> I wrote only <code>require('foo.php')</code></p>

<p>That error is really hard to spot, especially if you have many lines of code.</p>

<p>Now <strong>should declaration of class throw an error?</strong></p>

<p>If I would get error on the line <code>$foo = new Foo();</code> the error would be easy to spot. In this case you get <code>Fatal error: Call to undefined method Foo::bar()</code> which makes you think: "But I declared the <strong>method</strong>" and you forget that the error ight be somewhere else.</p>

<p>So, <strong>why does not PHP throw any error on creating new class which does not exist?</strong></p>