PHP变量,方法,类等名称中的有效字符是什么? [英] What are the valid characters in PHP variable, method, class, etc names?

查看:86
本文介绍了PHP变量,方法,类等名称中的有效字符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP名称中可用于变量,常量,函数,方法,类等的有效字符是什么?

What are the valid characters you can use in PHP names for variables, constants, functions, methods, classes, ...?

该手册包含一些

The manual has some mentions of the regular expression [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*. When does this restriction apply and when does it not?

推荐答案

仅当在某些特殊的语法元素中直接使用该名称时,[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*正则表达式才适用.一些例子:

The [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* regex only applies when the name is used directly in some special syntactical element. Some examples:

$varName           // <-- varName needs to satisfy the regex
$foo->propertyName // <-- propertyName needs to satisfy the regex
class ClassName {} // <-- ClassName needs to satisfy the regex
                   //     and can't be a reserved keyword

请注意,此正则表达式逐字节应用,不考虑编码.这就是为什么允许许多奇怪的Unicode名称.

Note that this regex is applied byte-per-byte, without consideration for encoding. That's why it also allows many weird Unicode names.

但是正则表达式仅 限制名称的这些直接"使用.通过PHP的各种动态功能,可以使用几乎任意名称.

But the regex restricts only these "direct" uses of names. Through various dynamic features PHP provides it's possible to use virtually arbitrary names.

通常,您不应做任何假设,以了解PHP中可以包含哪些字符名称.在大多数情况下,它们只是任意字符串.在PHP中执行诸如验证某物是否为有效的类名"之类的事情是毫无意义的.

In general you should make no assumptions about which characters names in PHP can contain. To the most parts they are just arbitrary strings. Doing things like "validating if something is a valid class name" is just meaningless in PHP.

下面,我将提供一些示例,说明如何为不同类别创建怪异名称.

In the following I will provide examples of how you can create weird names for the different categories.

变量名称可以是任意字符串:

Variable names can be arbitrary strings:

${''} = 'foo';
echo ${''};      // foo
${"\0"} = 'bar';
echo ${"\0"};    // bar

常量

全局常量也可以是任意字符串:

Constants

Global constants can also be arbitrary strings:

define('', 'foo');
echo constant('');   // foo
define("\0", 'bar');
echo constant("\0"); // bar

没有办法动态定义我知道的类常量,因此它们不能是任意的.创建怪异的类常量的唯一方法似乎是通过扩展代码.

There is no way to dynamically define class constants that I'm aware of, so those can not be arbitrary. Only way to create weird class constants seems to be via extension code.

属性不能为空字符串,也不能以NUL字节开头,但除此之外,它们是任意的:

Properties can not be the empty string and can not start with a NUL byte, but apart from this they are arbitrary:

$obj = new stdClass;
$obj->{''} = 'foo';   // Fatal error: Cannot access empty property
$obj->{"\0"} = 'foo'; // Fatal error: Cannot access property started with '\0'
$obj->{'*'} = 'foo';
echo $obj->{'*'};     // foo

方法

方法名称是任意的,可以用__call魔术来处理:

class Test {
    public function __call($method, $args) {
        echo "Called method \"$method\"";
    }
}

$obj = new Test;
$obj->{''}();    // Called method ""
$obj->{"\0"}();  // Called method "\0"

可以使用class_alias创建任意的类名,但空字符串除外:

Classes

Arbitrary class names can be created using class_alias with the exception of the empty string:

class Test {}

class_alias('Test', '');
$className = '';
$obj = new $className; // Fatal error: Class '' not found

class_alias('Test', "\0");
$className = "\0";
$obj = new $className; // Works!

功能

我不知道从用户区创建任意函数名称的方法,但是在某些情况下,内部代码会产生怪异"名称:

Functions

I'm not aware of a way to create arbitrary function names from userland, but there are still some occasions where internal code produces "weird" names:

var_dump(create_function('',''));
// string(9) "\0lambda_1"

这篇关于PHP变量,方法,类等名称中的有效字符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆