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?)
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?)
Interfaces must be declared before being referenced.
Joe
That's not the problem, the problem is when you extend a class with a
(defined) interface, you can't use the class before it is defined (the
class, not the interface). See the examples on the linked question.
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?)Interfaces must be declared before being referenced.
Joe
That's not the problem, the problem is when you extend a class with a
(defined) interface, you can't use the class before it is defined (the
class, not the interface). See the examples on the linked question.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?)Interfaces must be declared before being referenced.
Joe
It is a logical extension, you declare a class to implement an interface
it is subject to the same rules as declaring an interface - ... it must
be declared before it is used.
On Fri, Apr 5, 2013 at 12:39 PM, Madara Uchiha dor.tchizik@gmail.comwrote:
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