php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62547 callback of private function in parent class works but static vars are lost
Submitted: 2012-07-13 12:01 UTC Modified: 2020-10-07 12:54 UTC
Votes:3
Avg. Score:3.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: beat at joomlapolis dot com Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.3.14 OS: osX 10.6.8
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: beat at joomlapolis dot com
New email:
PHP Version: OS:

 

 [2012-07-13 12:01 UTC] beat at joomlapolis dot com
Description:
------------
In the case below where the callback function is private, its static local 
variable is lost during call of inherited class,

t if the callback function is private as below, it still gets called, but the 
local static variable is null instead of initialized like in PHP 5.2, where it 
works.

Also if you make the callback function "protected" instead of "private", the 
result is correct, as expected.

This is reproducible at least on PHP 5.3.8.

Test script:
---------------
class ok {
	public function t() {
		$this->replacer( '*' );
		return preg_replace_callback( '/(t)/', array( $this, 'replacer'), 'this is a simple test' );
	}
	private function replacer( $input ) {
		static $cache					=	'-';
		if ( ! is_array( $input ) ) {
			$cache						=	$input;
		} else {
			return $cache . $input[1] . $cache;
		}
	}
}
class buggyphp extends ok {}

class buggyphptoo extends ok {
	public function t() {
		return parent::t();
	}
}

$buggy = new buggyphp();
echo 'Result buggyphp: ' . $buggy->t();
echo "\n";
$buggy = new buggyphptoo();
echo 'Result buggyphptoo: ' . $buggy->t();
echo "\n";
$a = new ok();
echo 'Result ok: ' . $a->t();
exit();


Expected result:
----------------
Result buggyphp: *t*his is a simple *t*es*t*
Result buggyphptoo: *t*his is a simple *t*es*t*
Result ok: *t*his is a simple *t*es*t*

Actual result:
--------------
Result buggyphp: -t-his is a simple -t-es-t-
Result buggyphptoo: -t-his is a simple -t-es-t-
Result ok: *t*his is a simple *t*es*t*

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-28 11:50 UTC] nikic@php.net
-Status: Open +Status: Verified
 [2015-08-28 11:50 UTC] nikic@php.net
Still present: https://3v4l.org/cXfUJ HHVM does this right.
 [2020-10-07 12:54 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-10-07 12:54 UTC] cmb@php.net
Fixed as of PHP 7.4.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 19:01:31 2024 UTC