Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Using "_" as a class name alias is deprecated
--FILE--
<?php

class_alias('stdClass', '_');

class_alias('stdClass', 'Foo\\_');

?>
--EXPECTF--
Deprecated: Using "_" as a class alias is deprecated since 8.4 in %s on line %d

Deprecated: Using "_" as a class alias is deprecated since 8.4 in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Using "_" as a class name in use statements is deprecated
--FILE--
<?php

namespace Foo\Bar {
use stdClass as _;
}

namespace {
use stdClass as _;
}

?>
--EXPECTF--
Deprecated: Using "_" as a class name is deprecated in %s on line %d

Deprecated: Using "_" as a class name is deprecated in %s on line %d
Comment thread
TimWolla marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Using "_" as a constant name is deprecated
--FILE--
<?php

namespace Foo\Bar {
const _ = 5;
}

namespace {
const _ = 5;

define('_', 'x');

define('foo\\_', 'x');
}

?>
--EXPECTF--
Deprecated: Calling a constant "_" is deprecated since 8.6 in %s on line %d

Deprecated: Calling a constant "_" is deprecated since 8.6 in %s on line %d

Deprecated: Calling a constant "_" is deprecated since 8.6 in %s on line %d

Warning: Constant _ already defined, this will be an error in PHP 9 in %s on line %d

Deprecated: Calling a constant "_" is deprecated since 8.6 in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Using "_" as a constant in use statements is deprecated
--FILE--
<?php

namespace Foo\Bar {
use const PHP_INT_MAX as _;
}

namespace {
use const PHP_INT_MAX as _;
}

?>
--EXPECTF--
Deprecated: Using "_" as a constant name is deprecated since 8.6 in %s on line %d

Deprecated: Using "_" as a constant name is deprecated since 8.6 in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Using "_" as a function name is NOT deprecated
--SKIPIF--
<?php
if (extension_loaded('gettext')) {
die("skip gettext extension defines the _ function");
}
?>
--FILE--
<?php

namespace Foo\Bar {
function _() {}
}

namespace {
function _() {}
echo "OK";
}

?>
--EXPECT--
OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Using "_" as a function in use statements is NOT deprecated
--FILE--
<?php

namespace Foo\Bar {
use function strlen as _;
}

namespace {
use function strlen as _;

echo "OK";
}

?>
--EXPECT--
OK
16 changes: 15 additions & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void zend_assert_valid_class_name(const zend_string *name, const char *type) /*
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
}
if (zend_string_equals_literal(name, "_")) {
if (zend_string_equals_literal(name, "_") || zend_string_ends_with_literal(name, "\\_")) {
zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
}
}
Expand Down Expand Up @@ -10213,6 +10213,20 @@ static void zend_compile_use(zend_ast *ast) /* {{{ */
"is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
}

if (zend_string_equals(new_name, ZSTR_CHAR('_'))) {
switch (type) {
case ZEND_SYMBOL_CLASS:
zend_error(E_DEPRECATED, "Using \"_\" as a class name is deprecated");
break;
case ZEND_SYMBOL_CONST:
zend_error(E_DEPRECATED, "Using \"_\" as a constant name is deprecated since 8.6");
break;
case ZEND_SYMBOL_FUNCTION:
break;
default: ZEND_UNREACHABLE();
}
}

if (current_ns) {
zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
Expand Down
6 changes: 6 additions & 0 deletions Zend/zend_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,12 @@ ZEND_API zend_constant *zend_register_constant(zend_constant *c)

c->attributes = NULL;

if (
zend_string_equals(name, ZSTR_CHAR('_'))
Comment thread
Girgias marked this conversation as resolved.
|| (slash && zend_string_ends_with_literal(name, "\\_"))
) {
zend_error(E_DEPRECATED, "Calling a constant \"_\" is deprecated since 8.6");
}
/* Check if the user is trying to define any special constant */
if (zend_string_equals_literal(name, "__COMPILER_HALT_OFFSET__")
|| (!persistent && zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name)))
Expand Down
Loading