I am not sure if this is a bug, a feature behaving in a desired but confusing 
way, or a feature behaving in a confusing and thus undesireable way.  I am 
therefore reporting it here in order to defer to those who know the answer to 
such questions better.

Consider the following:

class Ancestor {
    public function __construct(int $a, string $b) { }
}

class Child extends Ancestor {
    public function __construct(...$args) {
        parent::__construct(...$args);
    }
}

This works with no compile errors.  For any other method however:

class Ancestor {
    public function doStuff(int $a, string $b) { }
}

class Child extends Ancestor {
    public function doStuff(...$args) {
        parent::doStuff(...$args);
    }
}

I get:

Warning: Declaration of Child::doStuff(...$args) should be compatible with 
Ancestor::doStuff(int $a, string $b)

I am not clear on why __construct() is special in this case; I know 
__construct() is special where interfaces are concerned, but I didn't realize 
it was special with regards to basic inheritance.  It seems to happen 
regardless of the type information presented (or not).

Is this intentional?  Is there a logical way it could be made to work?  Are 
constructors actually wrong here?  (I hope not, because it's a neat trick.)

Additionally, according to 3v4l.org at least, the error message has changed.  
On 7.1-7.3, the exact error message is:

Warning: Declaration of Child::doStuff(int ...$args) should be compatible with 
Ancestor::doStuff($a, $b) in /in/6NthP on line 15

On 7.4, it reports on a different line:

Warning: Declaration of Child::doStuff(int ...$args) should be compatible with 
Ancestor::doStuff($a, $b) in /in/6NthP on line 11

Specifically, prior to 7.4, it reports on the LAST line of the class (the 
closing brace).  As of 7.4 it reports on the line of the method that is 
inconsistent.

I don't think this is a bad change, per se.  It's actually a good change for 
debugging, IMO.  But I don't see it listed in the migration guide.  Should it 
be, in case some failure-mode tests or other automated error systems care?

https://www.php.net/manual/en/migration74.incompatible.php

-- 
  Larry Garfield
  la...@garfieldtech.com

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to