Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Don't use separate static variables in inherited methods #6719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Zend/tests/anon/015.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ var_dump($d->foo(24));
var_dump($c->foo());
?>
--EXPECT--
NULL
array(1) {
[0]=>
int(42)
}
array(1) {
[0]=>
int(24)
}
array(1) {
[0]=>
int(42)
int(24)
}
9 changes: 6 additions & 3 deletions Zend/tests/anon/016.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ var_dump($d->foo(24));
var_dump($c->foo());
?>
--EXPECT--
NULL
array(1) {
[0]=>
object(stdClass)#2 (0) {
}
}
array(1) {
[0]=>
int(24)
}
array(1) {
[0]=>
object(stdClass)#2 (0) {
}
int(24)
}
4 changes: 2 additions & 2 deletions Zend/tests/method_static_var.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ Bar::test();
--EXPECT--
int(1)
int(2)
int(1)
int(2)
int(3)
int(4)
43 changes: 43 additions & 0 deletions Zend/tests/static_variables_traits.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Behavior of static variables in trait methods
--FILE--
<?php

trait T {
public static function counter() {
static $i = 0;
var_dump(++$i);
}
}

class C1 {
use T {
T::counter as counter1;
T::counter as counter2;
}
}

class C2 {
use T;
}

C1::counter();
C1::counter1();
C1::counter2();
C2::counter();

C1::counter();
C1::counter1();
C1::counter2();
C2::counter();

?>
--EXPECT--
int(1)
int(1)
int(1)
int(1)
int(2)
int(2)
int(2)
int(2)
24 changes: 4 additions & 20 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,7 @@ static zend_function *zend_duplicate_internal_function(zend_function *func, zend
}
/* }}} */

static zend_function *zend_duplicate_user_function(zend_function *func) /* {{{ */
{
zend_op_array *new_op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(new_op_array, func, sizeof(zend_op_array));
zend_init_static_variables_map_ptr(new_op_array);
return (zend_function *) new_op_array;
}
/* }}} */

static zend_always_inline zend_function *zend_duplicate_function(zend_function *func, zend_class_entry *ce, bool is_interface) /* {{{ */
static zend_always_inline zend_function *zend_duplicate_function(zend_function *func, zend_class_entry *ce) /* {{{ */
{
if (UNEXPECTED(func->type == ZEND_INTERNAL_FUNCTION)) {
return zend_duplicate_internal_function(func, ce);
Expand All @@ -108,12 +99,7 @@ static zend_always_inline zend_function *zend_duplicate_function(zend_function *
if (EXPECTED(func->op_array.function_name)) {
zend_string_addref(func->op_array.function_name);
}
if (is_interface
|| EXPECTED(!func->op_array.static_variables)) {
/* reuse the same op_array structure */
return func;
}
return zend_duplicate_user_function(func);
return func;
}
}
/* }}} */
Expand Down Expand Up @@ -948,9 +934,7 @@ static zend_always_inline inheritance_status do_inheritance_check_on_method_ex(

if (!check_only && child->common.prototype != proto && child_zv) {
do {
if (child->common.scope != ce
&& child->type == ZEND_USER_FUNCTION
&& !child->op_array.static_variables) {
if (child->common.scope != ce && child->type == ZEND_USER_FUNCTION) {
if (ce->ce_flags & ZEND_ACC_INTERFACE) {
/* Few parent interfaces contain the same method */
break;
Expand Down Expand Up @@ -1021,7 +1005,7 @@ static zend_always_inline void do_inherit_method(zend_string *key, zend_function
ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
}

parent = zend_duplicate_function(parent, ce, is_interface);
parent = zend_duplicate_function(parent, ce);

if (!is_interface) {
_zend_hash_append_ptr(&ce->function_table, key, parent);
Expand Down
6 changes: 3 additions & 3 deletions ext/opcache/tests/preload_method_static_vars.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Bar::test();
--EXPECT--
int(1)
int(2)
int(1)
int(2)
int(3)
int(4)

int(1)
int(1)
int(2)
1 change: 0 additions & 1 deletion ext/opcache/zend_persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ static void zend_persist_class_method(zval *zv, zend_class_entry *ce)
}

if ((op_array->fn_flags & ZEND_ACC_IMMUTABLE)
&& !op_array->static_variables
&& !ZCG(current_persistent_script)->corrupted
&& zend_accel_in_shm(op_array)) {
zend_shared_alloc_register_xlat_entry(op_array, op_array);
Expand Down