PHP5: Class behavior: method overloading

php.internals

Vivian Steller

22 years ago
Hello, in the latest snapshot i found some very important differences to the php5 beta 4 version concerning the behavior of inherited classes. please look at the following example: <pre> <?php class MyClass extends MyParent{ function test($arg) { } } class MyParent { function test($arg1, $arg2) { } } $obj = new MyClass(); ?> </pre> the output is: Fatal error: Declaration of MyClass::test() must be compatible with that of MyParent::test() in ... on line 3 I think that this is a nice feature in the face of "compatibility", but may cause in a complete restructure of existing class trees. The consequence is a bad :( backward compatibility!! Personally I think it would be a good solution to set an option in the configuration or something like this. other oppinions would be appreciated. thanks, vivi

Walt Boring

22 years ago
I say keep compatibility. If you want to enforce this, then declare an interface. That is after all why they exist. Walt

Vivian Steller

22 years ago
Walter A. Boring IV wrote:
> I say keep compatibility. If you want to enforce this, then declare an > interface. That is after all why they exist. > > Walt
... Would be nice if you could give a simple example - i really have problems working with interfaces :( My problem was to design a filesystem structure with classes like this: class Folder { var $name; var $path; function init() {} // should set $name and $path... function read($recursive = FALSE) {} // should read own content and eventually all subdirs } class File extends Folder { // inherited properties and funcs... function read() {} // should read the file content, $recursive is not needed } how could i implement my interfaces? I think interfaces wouldn't solve php4 compatibility problems... best regards, vivi

Walt Boring

22 years ago
Quoting Vivian Steller <news@eecoo.de>:
> Walter A. Boring IV wrote: > > > I say keep compatibility. If you want to enforce this, then declare an > > interface. That is after all why they exist. > > > > Walt > ... > > Would be nice if you could give a simple example - i really have problems > working with interfaces :(
interface Entry function __construct($name, $path); } class Folder implements Entry { var $name = ''; var $path = ''; function __construct( $name, $path ) { $this->name = $name; $this->path = $path; } }
> > > how could i implement my interfaces? I think interfaces wouldn't solve php4 > compatibility problems...
----well, if you are implementing something completely new in php5, there there are no compatibility issues. I have many classes in my projects in php4. I'm not sure of how many child constructors have different parameters (my guess is not many), but enforcing strict parameter count for childs classes would break compatibility. Which is why I think that one should just use an interface to enforce this rule. It seems like the right place for it. my $0.02 Walt

Cristiano Duarte

22 years ago
Hi Walter, On Thu, 26 Feb 2004 11:44:47 -0800, Walter A. Boring IV wrote:
> ----well, if you are implementing something completely new in php5, > there there are no compatibility issues. I have many classes in my > projects in php4. I'm not sure of how many child constructors have > different parameters (my guess is not many), but enforcing strict > parameter count for childs classes would break compatibility. Which is > why I think that one should just use an interface to enforce this rule. > It seems like the right place for it.
In the object oriented point of view, there are no differences between an interface and a class when you use something like this: ... } catch (MyException $e) { ... So, MyException is a "class" or an "interface" ? You can't tell. That's why this enforcement is applied over classes too. A class definition is still a commitment of "interfacing". When you extend the base class you are not supposed to break this commitment. If you allow this commitment break, it may lead to "buggy" code when you take a class by its superclass "interface". Best Regards, Cristiano Duarte

Derick Rethans

22 years ago
On Thu, 26 Feb 2004, Vivian Steller wrote:
> Fatal error: Declaration of MyClass::test() must be compatible with that of > MyParent::test() in ... on line 3 > > > I think that this is a nice feature in the face of "compatibility", but may > cause in a complete restructure of existing class trees. The consequence is > a bad :( backward compatibility!! Personally I think it would be a good > solution to set an option in the configuration or something like this.
I'm afraid it even people without using a release already run into these problems the first day, then it is probably an issue for a LOT of people. Pretty please put this in E_STRICT? regards, Derick