Re: Inconsistency in class definition/usage order

From: Date: Fri, 05 Apr 2013 11:12:11 +0000
Subject: Re: Inconsistency in class definition/usage order
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-66976@lists.php.net to get a copy of this message
On Fri, Apr 5, 2013 at 12:39 PM, Madara Uchiha <dor.tchizik@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

« previous php.internals (#66976) next »