Bug #74998 [NEW]: callable typehint generates redundant and unnecessary deprecation warning
| From: | alex dot howansky at gmail dot com | Date: | Thu, 27 Jul 2017 21:07:10 +0000 |
| Subject: | Bug #74998 [NEW]: callable typehint generates redundant and unnecessary deprecation warning | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-210375@lists.php.net to get a copy of this message | ||
From: alex dot howansky at gmail dot com
Operating system: Fedora 25
PHP version: 7.1.7
Package: Scripting Engine problem
Bug Type: Bug
Bug description:callable typehint generates redundant and unnecessary deprecation warning
Description:
------------
If you have a function/method that takes an argument typehinted with
callable, and you pass it an array of static strings which references a
method that's not defined as static, you'll get a deprecation warning
about making a static call to a non-static method -- even if you never
actually invoke the callable. If you then invoke the callable, you'll
get an identical second deprecation warning.
Note the test script. As is, it generates two identical warnings, on
adjacent lines, one for the callable typehint, and one for the actual
call. The first is unwanted and unnecessary. The warning should be
triggered only when I actually make the deprecated call. Simply passing
a static callable should not trigger the warning. I.e., PHP shouldn't
warn me about something I might possibly do in the future, it should
warn me only about things I've actually done.
Since the invocation will always generate a warning, there's no need to
also generate one when validating the typehint. Otherwise, you will
always get two identical warnings for a single issue.
Suppose I add some conditional logic to this method, to detect if the
callable is static and automatically instantiate an object for it if
so:
if (is_string($callable[0])) {
call_user_func([new $callable[0], $callable[1]]);
} else {
call_user_func($callable);
}
This should not generate any deprecation warnings, as nothing would ever
get called statically. As it stands, in order to get rid of the
warnings, I would have to remove the callable typehint.
Test script:
---------------
<?php
error_reporting(E_ALL);
class Caller {
public function call(callable $callable) {
call_user_func($callable);
}
}
class Foo {
public function bar() {
echo "here\n";
}
}
$caller = new Caller();
$callable = ['Foo', 'bar'];
$caller->call($callable);
Expected result:
----------------
PHP Deprecated: Non-static method Foo::bar() should not be called
statically in /tmp/foo.php on line 7
here
Actual result:
--------------
PHP Deprecated: Non-static method Foo::bar() should not be called
statically in /tmp/foo.php on line 6
PHP Deprecated: Non-static method Foo::bar() should not be called
statically in /tmp/foo.php on line 7
here
--
Edit bug report at https://bugs.php.net/bug.php?id=74998&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74998&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74998&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74998&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=74998&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=74998&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=74998&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=74998&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=74998&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=74998&r=support
Expected behavior: https://bugs.php.net/fix.php?id=74998&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=74998&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=74998&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=74998&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74998&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=74998&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=74998&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=74998&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74998&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=74998&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=74998&r=mysqlcfg