Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class implementing an interface must be declared before a children class?

I stumbled on an interesting issue today. In the context of a single file for multiple declarations, if class B implements interface A, and class C extends class A, class B must be declared before class C.

The following code does not work:

interface A {}
class C extends B {} // Class 'B' not found
class B implements A {}

This fixes it:

interface A {}
class B implements A {}
class C extends B {} // Class 'B' is found

But this works fine:

class A {}
class C extends B {} // Class 'B' is found
class B extends A {}

These are my results on PHP 5.3.8 (Win32) and PHP 5.3.3-1ubuntu9.6 w/ suhosin.

So the question is, is this documented behavior? Why would it work with classes but not interfaces? Or should this be considered a bug?

Let me know about your experience, before I go digging in the PHP's source code and/or open a PHP bug ticket. :)

Thanks!

Note: I know it's just a matter of class declaration order, but this puzzles me... If it's inappropriate, please don't hesitate to close.

like image 299
netcoder Avatar asked Dec 19 '11 16:12

netcoder


People also ask

What must a class do in order to implement an interface?

To implement an interface, a class must specify that implements that interface and it must also provide implementations for all of the methods defined in the interface. If a class fails to implement one or more abstract methods from the interface, the compiler will complain.

Does a child class have to implement all interface methods?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class. Implement every method defined by the interface.

When an interface method is implemented in a class it must be declared as?

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.

When an interface method is implemented it must be declared as public?

Interface methods are implicitly public in C# because an interface is a contract meant to be used by other classes. In addition, you must declare these methods to be public, and not static, when you implement the interface.


1 Answers

Yes, this is documented behavior:

Object Inheritance (Note):

Unless autoloading is used, then classes must be defined before they are used. If a class extends another, then the parent class must be declared before the child class structure. This rule applies to class that inherit other classes and interfaces.

 

PHP: extends - Manual (Note)

Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first. If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first. To make it short: the order in which the classes are defined is important.


Undefined Behavior (with added sarcasm)

Even though your third example might word, it's not said to work according to the documentation (and I cannot find any changelog entry stating that it's a feature brought in).

The documentation of PHP sadly isn't 100%, but you can only trust what you know, and the above quotes are all I can find regarding this matter.

You might call it a bug, but in C++ we would describe it as undefined behavior ie. something that shouldn't be relied on.


If the "standard" (documentation) doesn't mention it, it's not safe to use.

If the "standard" (documentation) doesn't mention it, it may make the universe implode - or a raptor jump through your window. Do not do anything that can kill us all!

like image 193
Filip Roséen - refp Avatar answered Sep 23 '22 07:09

Filip Roséen - refp