On Fri, Apr 5, 2013 at 12:39 PM, Madara Uchiha <dor.tchi...@gmail.com>wrote:

> After I saw this question on Stack Overflow:
>
> http://stackoverflow.com/questions/15688642/how-does-a-class-extension-or-interface-work
> ,
> I realized that the guy was right.
>
> Is there an explanation for this, or is it just one of those things
> that got overlooked?
>
> Should something be done? (i.e. enforcing class usage only after
> definition?)
>

Did you also read the answer to that question? I think it does a good job
at explaining why it is not possible. Extending a class or implementing an
interface makes the class definition "conditional", because it now has a
dependency on the extended class or implemented interface. To make it
clearer, consider this example:

<?php
var_dump(new Foo);
if ($abc) {
    include 'foo/SomeInterface.php';
} else {
    include 'bar/SomeInterface.php';
}
class Foo implements SomeInterface {}

In this example, if we allowed using classes with extends/implements before
their definition, which interface would the "new Foo" object created at the
start implement? The one from the foo/ directory, or the one from the bar/
directory? We can't know that unless we execute the code before the
definition first. We can only allow use-before-declaration is the
implemented interface / extended class is already known. No way to change
that.

Nikita

Reply via email to