php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74292 Warning of incompatible declaration for ReflectionClass::newInstance()
Submitted: 2017-03-22 17:34 UTC Modified: 2018-07-02 18:15 UTC
Votes:4
Avg. Score:3.8 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:2 (50.0%)
From: ramsey@php.net Assigned: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: master-Git-2017-03-22 (Git) OS:
Private report: No CVE-ID: None
 [2017-03-22 17:34 UTC] 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(
))

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-22 17:41 UTC] 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-04-01 16:21 UTC] php at duncanc dot co dot uk
https://bugs.php.net/bug.php?id=74035
 [2017-06-27 16:39 UTC] lisachenko dot it at gmail dot com
BC break can be avoided in children classes with special signature hack, see https://3v4l.org/eeVf8 for details.
 [2017-11-02 16:07 UTC] bugs dot php dot net at majkl578 dot cz
This is now a BC break in PHP 7.2 RC.

Related:
https://github.com/php/php-src/pull/2363
https://github.com/php/php-src/commit/9ffc6ca62f53431a4b32b30cdda8180142f47cdb
https://github.com/doctrine/common/pull/822
 [2018-07-02 18:15 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2018-07-02 18:15 UTC] nikic@php.net
This change was reverted prior to the PHP 7.2 release, so closing this bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 15:01:30 2024 UTC