Skip to content

Commit 6f76b17

Browse files
committed
Added E_STRICT warning in case of calling non-static method from incompatible context (this feature is staying for BC with php-4).
1 parent 7865dfb commit 6f76b17

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Zend/zend_vm_def.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,15 @@ ZEND_VM_HANDLER(113, ZEND_INIT_STATIC_METHOD_CALL, ANY, CONST|TMP|VAR|UNUSED|CV)
17231723
if (EX(fbc)->common.fn_flags & ZEND_ACC_STATIC) {
17241724
EX(object) = NULL;
17251725
} else {
1726+
if (OP2_TYPE != IS_UNUSED &&
1727+
EG(This) &&
1728+
Z_OBJ_HT_P(EG(This))->get_class_entry &&
1729+
!instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
1730+
/* We are calling method of the other (incompatible) class,
1731+
but passing $this. This is done for compatibility with php-4. */
1732+
zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically, assuming $this from incompatible context", EX(fbc)->common.scope->name, EX(fbc)->common.function_name);
1733+
1734+
}
17261735
if ((EX(object) = EG(This))) {
17271736
EX(object)->refcount++;
17281737
}

Zend/zend_vm_execute.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,15 @@ static int ZEND_INIT_STATIC_METHOD_CALL_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_A
678678
if (EX(fbc)->common.fn_flags & ZEND_ACC_STATIC) {
679679
EX(object) = NULL;
680680
} else {
681+
if (IS_CONST != IS_UNUSED &&
682+
EG(This) &&
683+
Z_OBJ_HT_P(EG(This))->get_class_entry &&
684+
!instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
685+
/* We are calling method of the other (incompatible) class,
686+
but passing $this. This is done for compatibility with php-4. */
687+
zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically, assuming $this from incompatible context", EX(fbc)->common.scope->name, EX(fbc)->common.function_name);
688+
689+
}
681690
if ((EX(object) = EG(This))) {
682691
EX(object)->refcount++;
683692
}
@@ -870,6 +879,15 @@ static int ZEND_INIT_STATIC_METHOD_CALL_SPEC_TMP_HANDLER(ZEND_OPCODE_HANDLER_ARG
870879
if (EX(fbc)->common.fn_flags & ZEND_ACC_STATIC) {
871880
EX(object) = NULL;
872881
} else {
882+
if (IS_TMP_VAR != IS_UNUSED &&
883+
EG(This) &&
884+
Z_OBJ_HT_P(EG(This))->get_class_entry &&
885+
!instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
886+
/* We are calling method of the other (incompatible) class,
887+
but passing $this. This is done for compatibility with php-4. */
888+
zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically, assuming $this from incompatible context", EX(fbc)->common.scope->name, EX(fbc)->common.function_name);
889+
890+
}
873891
if ((EX(object) = EG(This))) {
874892
EX(object)->refcount++;
875893
}
@@ -1019,6 +1037,15 @@ static int ZEND_INIT_STATIC_METHOD_CALL_SPEC_VAR_HANDLER(ZEND_OPCODE_HANDLER_ARG
10191037
if (EX(fbc)->common.fn_flags & ZEND_ACC_STATIC) {
10201038
EX(object) = NULL;
10211039
} else {
1040+
if (IS_VAR != IS_UNUSED &&
1041+
EG(This) &&
1042+
Z_OBJ_HT_P(EG(This))->get_class_entry &&
1043+
!instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
1044+
/* We are calling method of the other (incompatible) class,
1045+
but passing $this. This is done for compatibility with php-4. */
1046+
zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically, assuming $this from incompatible context", EX(fbc)->common.scope->name, EX(fbc)->common.function_name);
1047+
1048+
}
10221049
if ((EX(object) = EG(This))) {
10231050
EX(object)->refcount++;
10241051
}
@@ -1167,6 +1194,15 @@ static int ZEND_INIT_STATIC_METHOD_CALL_SPEC_UNUSED_HANDLER(ZEND_OPCODE_HANDLER_
11671194
if (EX(fbc)->common.fn_flags & ZEND_ACC_STATIC) {
11681195
EX(object) = NULL;
11691196
} else {
1197+
if (IS_UNUSED != IS_UNUSED &&
1198+
EG(This) &&
1199+
Z_OBJ_HT_P(EG(This))->get_class_entry &&
1200+
!instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
1201+
/* We are calling method of the other (incompatible) class,
1202+
but passing $this. This is done for compatibility with php-4. */
1203+
zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically, assuming $this from incompatible context", EX(fbc)->common.scope->name, EX(fbc)->common.function_name);
1204+
1205+
}
11701206
if ((EX(object) = EG(This))) {
11711207
EX(object)->refcount++;
11721208
}
@@ -1248,6 +1284,15 @@ static int ZEND_INIT_STATIC_METHOD_CALL_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS
12481284
if (EX(fbc)->common.fn_flags & ZEND_ACC_STATIC) {
12491285
EX(object) = NULL;
12501286
} else {
1287+
if (IS_CV != IS_UNUSED &&
1288+
EG(This) &&
1289+
Z_OBJ_HT_P(EG(This))->get_class_entry &&
1290+
!instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
1291+
/* We are calling method of the other (incompatible) class,
1292+
but passing $this. This is done for compatibility with php-4. */
1293+
zend_error(E_STRICT, "Non-static method %s::%s() should not be called statically, assuming $this from incompatible context", EX(fbc)->common.scope->name, EX(fbc)->common.function_name);
1294+
1295+
}
12511296
if ((EX(object) = EG(This))) {
12521297
EX(object)->refcount++;
12531298
}

0 commit comments

Comments
 (0)