Bug #74292 [Com]: Warning of incompatible declaration for ReflectionClass::newInstance()

From: Date: Tue, 27 Jun 2017 16:39:34 +0000
Subject: Bug #74292 [Com]: Warning of incompatible declaration for ReflectionClass::newInstance()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209708@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74292&edit=1

 ID:                 74292
 Comment by:         lisachenko dot it at gmail dot com
 Reported by:        ramsey@php.net
 Summary:            Warning of incompatible declaration for
                     ReflectionClass::newInstance()
 Status:             Open
 Type:               Bug
 Package:            Reflection related
 PHP Version:        master-Git-2017-03-22 (Git)
 Block user comment: N
 Private report:     N

 New Comment:

BC break can be avoided in children classes with special signature hack, see https://3v4l.org/eeVf8 for details.


Previous Comments:
------------------------------------------------------------------------
[2017-04-01 16:21:56] php at duncanc dot co dot uk

https://bugs.php.net/bug.php?id=74035

------------------------------------------------------------------------
[2017-03-22 17:41:00] kelunik@php.net

The documented signature is variadic, but it seems like the signature wasn't implemented like
that before. The behavior in PHP 7.2 is the right one based on the documentation, but I'm not
sure it's worth the BC break.

------------------------------------------------------------------------
[2017-03-22 17:34:21] ramsey@php.net

Description:
------------
In PHP 7.2.0-dev, when extending ReflectionClass and overriding the newInstance() method, I am
receiving a warning about an incompatible declaration (see actual result for warning message).

In PHP 5.6.30 and 7.1.3, I do not receive this warning. This is a possible BC break in 7.2.

This warning turned up in the PHP nightlies on Travis CI for packages depending on the
goaop/parser-reflection library. See here for details: https://github.com/goaop/parser-reflection/issues/70

I have confirmed the problem locally using the following test script, tested against 5.6.30, 7.1.3,
and 7.2.0-dev (built 2017-03-22 from master).

Test script:
---------------
<?php

class Foo {}

class MyReflectionClass extends \ReflectionClass
{
    public function newInstance($args = null)
    {
        return call_user_func_array('parent::newInstance', func_get_args());
    }
}

$fooReflected = new MyReflectionClass(Foo::class);
$fooInstance = $fooReflected->newInstance();

var_export($fooInstance);

Expected result:
----------------
$ php reflection-class-error.php
Foo::__set_state(array(
))

Actual result:
--------------
$ php reflection-class-error.php
PHP Warning:  Declaration of MyReflectionClass::newInstance($args = NULL) should be compatible with
ReflectionClass::newInstance(...$args) in /Users/ramsey/Desktop/reflection-class-error.php on line
11

Warning: Declaration of MyReflectionClass::newInstance($args = NULL) should be compatible with
ReflectionClass::newInstance(...$args) in /Users/ramsey/Desktop/reflection-class-error.php on line
11
Foo::__set_state(array(
))


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=74292&edit=1


Thread (6 messages)

« previous php.bugs (#209708) next »