|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-12-12 08:33 UTC] someone dot is dot alive at gmail dot com
Description: ------------ Function has 2 arguments. 2nd has a default value. When calling a function it returns an fatal error instead passing second argument as a default value. Test script: --------------- Test case is available in zip https://www.jerc.si/wp-content/uploads/2018/12/php7.3.zip Contains: - index.php - init.php (load files) - PHPDemo/Utils.php (echo's few lines) - PHPDemo/TestConst.php (simple class instance) Short preview: public static function testInput(string $name, int $step = TestConst::STEP_1) Utils::testInput('abc 1'); Utils::testInput('abc 2', TestConst::STEP_2); Utils::testInput('abc 3'); Expected result: ---------------- Based on a code in ZIP file. "Hello world! abc 3 > 1 yes" Actual result: -------------- TypeError: Argument 2 passed to PHPDemo\Utils::testInput() must be of the type int, unknown given, called in D:\www\index.php on line 10 in D:\www\PHPDemo\Utils.php on line 6 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 02:00:01 2025 UTC |
Test script (below) is copied from zip file. <?php namespace PHPDemo; class TestConst { const STEP_1 = 1; const STEP_2 = 2; public static $persistent = true; private static $instance = null; private $currentStep = self::STEP_1; private function __construct() { } public function wasPreviousStepCompleted(int $currentStep) : bool { return $this->currentStep >= $currentStep; } public static function &instance() { if (session_id() == '') { session_start(); } if (is_null(self::$instance)) { self::$instance = new TestConst(); } if (self::$persistent) { $_SESSION['TestConst'] = self::$instance; } else { unset($_SESSION['TestConst']); } return self::$instance; } public static function destroyInstance() { self::$instance = null; } public function __wakeup() { self::$instance =& $this; } } class Utils { public static function testInput(string $name, int $step = TestConst::STEP_1) { var_dump(func_get_args()); if (empty($name)) { return; } echo 'Hello world! ' . $name . ' > ' . $step . '<br />'; $test = TestConst::instance(); if ($test->wasPreviousStepCompleted($step)) { echo 'yes'; } else { echo 'no'; } echo '<br />'; } } TestConst::instance(); Utils::testInput('abc 1'); Utils::testInput('abc 2', TestConst::STEP_2); Utils::testInput('abc 3');I think i found a problem - when opcache is enabled i get the error. Usually happens when i edit file (e.g. (un)comment TestConst::STEP_2) This is the shortest code to reproduce: - no exception at all (e.g. for first 10 requests or it may throw error on first attempt) - Argument 2 passed to PHPDemo\Utils::testInput() must be of the type int, unknown given - Argument 2 passed to PHPDemo\Utils::testInput() must be of the type int, object given - Argument 2 passed to PHPDemo\Utils::testInput() must be of the type int, bool given <?php namespace PHPDemo; class TestConst { const STEP_1 = 1; const STEP_2 = 2; } class Utils { public static function testInput(string $name, int $step = TestConst::STEP_1) { } } Utils::testInput('abc 1'); ------ I deleted all cached files, restarted server OPCache log (opcache.log_verbosity_level=4) Wed Dec 12 11:15:22 2018 (33644): Debug Restart Scheduled! Reason: user Wed Dec 12 11:15:22 2018 (33644): Debug Restarting! Wed Dec 12 11:17:53 2018 (33644): Message Cached script 'D:\www\demo.php' Wed Dec 12 11:15:45 2018 (33644): Message Added key 'D:/www/demo.php' Wed Dec 12 11:18:44 2018 (33644): Message Cached script 'D:\www\demo.php' Wed Dec 12 11:18:44 2018 (33644): Message Added key 'D:/www/demo.php'I've encountered this as well after upgrading from 7.2 to 7.3 on Debian Testing. Unfortunately, I can't seem to reproduce it in a minimal fashion; it only occurs in a project with rather large graph of inter-dependent objects. The bug manifests itself with class methods that use constants (can be a class constant, built-in constant or user-defined global constant) as default parameter values, i.e.: public function someMethod($some_param=SOME_CONSTANT) {} When someMethod is called with no arguments, $some_param *sometimes* (more often than not, but not always) receives a value that var_dump() identifies as NFC. Which, if passed down to a function or method that expects a specific type, throws a "[function] expects parameter [num] to be [type], unknown given" error OP mentions.