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

[email protected] ("php at duncanc dot co dot uk")
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=74292&edit=1

 ID:                 74292
 Comment by:         php at duncanc dot co dot uk
 Reported by:        [email protected]
 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:

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


Previous Comments:
------------------------------------------------------------------------
[2017-03-22 17:41:00] [email protected]

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] [email protected]

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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.