Re: [RFC] Default constructors

From: Date: Tue, 18 Nov 2014 20:30:52 +0000
Subject: Re: [RFC] Default constructors
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-78956@lists.php.net to get a copy of this message
Hi! > Does it *really* make sense for PHP? What if that parent class 'Animal' > from your example introduces a constructor that accepts, say, both > $owner and $what as mandatory > arguments? parent::__construct() Then you need to rewrite all descendant classes anyway, this would be API change, not an API-preserving refactoring. Essentially, you take the Animal class and replace it with entirely different class with different requirements. You'll have to rewrite all the code that constructs Animal objects. This obviously is not the scenario I'm talking about (and of course nowhere near recommended practice for any maintainable code). > call in a descendant will appear broken (unintentionally, I suppose), > won't it? That'll work in Java just fine, though. In Java, you can not do that - if your ctor requires 2 arguments, you'd have to give it 2 arguments. If you still have the default ctor, that'd be fine, and so it will be in PHP - but in PHP you can not have two ctor functions, so in PHP you do things differently, we use optional arguments instead of using two functions. The meaning is the same though - if you're able to call new Foo(), you should be able to call parent::__construct() (or super() in Java case) on Foo. Except in Java it works and in PHP it doesn't. That's what I want to fix.

« previous php.internals (#78956) next »