Skip to content

Commit 07877c4

Browse files
DanielCiochiunikic
DanielCiochiu
authored andcommitted
Fixed bug #75546
By respecting the SILENT flag when checking the visibility of a class constant.
1 parent 8e34de4 commit 07877c4

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PHP NEWS
55
- Core:
66
. Fixed bug #77589 (Core dump using parse_ini_string with numeric sections).
77
(Laruence)
8+
. Fixed bug #75546 (function "defined" should ignore class constant
9+
visibility). (Daniel Ciochiu)
810

911
- Exif:
1012
. Fixed bug #77564 (Memory leak in exif_process_IFD_TAG). (Ben Ramsey)

Zend/zend_constants.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
341341
ret_constant = NULL;
342342
} else {
343343
if (!zend_verify_const_access(c, scope)) {
344-
zend_throw_error(NULL, "Cannot access %s const %s::%s", zend_visibility_string(Z_ACCESS_FLAGS(c->value)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
344+
if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
345+
zend_throw_error(NULL, "Cannot access %s const %s::%s", zend_visibility_string(Z_ACCESS_FLAGS(c->value)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
346+
}
345347
goto failure;
346348
}
347349
ret_constant = &c->value;

ext/standard/tests/general_functions/bug72920.phpt

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ class Foo {
66
private const C1 = "a";
77
}
88

9-
try {
10-
var_dump(constant('Foo::C1'));
11-
} catch (Error $e) {
12-
var_dump($e->getMessage());
13-
}
14-
--EXPECT--
15-
string(35) "Cannot access private const Foo::C1"
9+
var_dump(constant('Foo::C1'));
10+
--EXPECTF--
11+
Warning: constant(): Couldn't find constant Foo::C1 in %s on line %d
12+
NULL

tests/classes/constants_visibility_002.phpt

+1-5
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,4 @@ constant('A::protectedConst');
2121
string(14) "protectedConst"
2222
string(14) "protectedConst"
2323

24-
Fatal error: Uncaught Error: Cannot access protected const A::protectedConst in %s:14
25-
Stack trace:
26-
#0 %s(14): constant('A::protectedCon...')
27-
#1 {main}
28-
thrown in %s on line 14
24+
Warning: constant(): Couldn't find constant A::protectedConst in %s on line %d

tests/classes/constants_visibility_003.phpt

+1-5
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,4 @@ constant('A::privateConst');
2121
string(12) "privateConst"
2222
string(12) "privateConst"
2323

24-
Fatal error: Uncaught Error: Cannot access private const A::privateConst in %s:14
25-
Stack trace:
26-
#0 %s(14): constant('A::privateConst')
27-
#1 {main}
28-
thrown in %s on line 14
24+
Warning: constant(): Couldn't find constant A::privateConst in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Defined on private constant should not raise exception
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
private const BAR = 1;
9+
}
10+
echo (int)defined('Foo::BAR');
11+
--EXPECTF--
12+
0

0 commit comments

Comments
 (0)