# Inconsistency in class definition/usage order

- Thread: https://externals.io/message/66970
- Original archive: https://news-web.php.net/php.internals/66970
- Started by Madara Uchiha on 2013-04-05T10:39:52+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #66970 — Madara Uchiha — 2013-04-05T10:39:52+00:00

URL: https://externals.io/message/66970#66970

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?)

---

## #66971 — Joe Watkins — 2013-04-05T10:48:20+00:00

URL: https://externals.io/message/66970#66971 · In reply to: #66970

> 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

---

## #66974 — Madara Uchiha — 2013-04-05T11:01:19+00:00

URL: https://externals.io/message/66970#66974 · In reply to: #66971

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.



> 
> > […]
> Interfaces must be declared before being referenced.
>
> Joe

---

## #66975 — Joe Watkins — 2013-04-05T11:09:57+00:00

URL: https://externals.io/message/66970#66975 · In reply to: #66974

> 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.
>
>
> 
> > […]
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.

---

## #66976 — Nikita Popov — 2013-04-05T11:12:11+00:00

URL: https://externals.io/message/66970#66976 · In reply to: #66970

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
