如何在 PHP 中创建新运算符?
我看到自己在 PHP 上为所有类型的默认分配执行以下代码,
$variable = $variable ? $variable : 'default value';
我知道在 5.3 中我可以做,
$variable = $variable ?: 'default value';
我想通过能够执行
$variable ?= 'default value';
并重新分配变量来进一步简化它,以防它的计算结果为 false。是否可以创建该作业?我必须编译我自己的 php 版本才能做到这一点吗?
I see myself doing the following code for default assigment all the type on PHP
$variable = $variable ? $variable : 'default value';
I know that with 5.3 I can do
$variable = $variable ?: 'default value';
I would like to further simplify it by being able to just do
$variable ?= 'default value';
and have the variable reassigned in case it evaluates to false. Is it possible to create that assignment? Do I have to compile my own version of php to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果不更改 PHP 源代码,则无法在 PHP 中创建新的运算符。
而且你不想改变它,相信我。当然,这很简单 - PHP 是开源的,并且使用 直接的 LALR (1) 解析器,您可以轻松修改它 - 但这会使您的代码与标准 PHP 实现不兼容。因此,您将非常严格地限制谁可以运行代码 - 除了您之外可能没有人,因为没有其他人会关心修改他们的 PHP 引擎只是为了运行您的代码。
更新:我写了一个关于如何向 PHP 添加新语法(如运算符)的小教程:https://www.npopov.com/2012/07/27/How-to-add-new-syntropic-features-to-PHP.html
You cannot create new operators in PHP without changing the PHP source.
And you don't want to change it, trust me. Sure, it'd be easy enough - PHP is open source and uses a straightforward LALR(1) parser which you could easily modify - but that would make your code incompatible with the standard PHP implementation. You would thus very much restrict who can run the code - which will probably be nobody apart from you, as nobody else will care to modify their PHP engine just to run your code.
Update: I wrote a small tutorial on how to add new syntax (like operators) to PHP: https://www.npopov.com/2012/07/27/How-to-add-new-syntactic-features-to-PHP.html
在 PHP 中,您无法创建运算符或重载现有运算符,例如
=
。您可以检查包 Operator,但是,如果没有它,您的代码将无法运行。
In PHP, you can't create operators or overload the existing operators, such as
=
.You can check the package Operator, but, your code will not be runnable withoud it.
你不能在 PHP 中执行此操作,不能使用此语法。
You cannot do this in PHP,not with this syntax.
当想知道是否可以创建新操作时,这似乎是最常出现的一个示例。我个人认为 ?= 会非常方便。
由于创建新运算符将涉及创建您自己的 PHP 版本,并且如果您决定打包代码,对其他人来说可能不是很有用,因此您可以轻松创建一个函数来模拟相同的效果。
然后,您可以调用该函数,根据需要将其分配给另一个变量,甚至根据需要嵌套它们以在变量层次结构中分配默认值。
编辑:
作为旁注,该函数还将为变量分配默认值,即使该变量尚未定义。
如果您希望仅更新未定义的变量,则可以创建类似的函数,或将上面的函数修改为以下函数。
It seems that this is the one example that comes up the most when wondering if it's possible to create new operations. I personally think that ?= would be quite handy.
Since creating new operators would involve creating your own PHP version, and may not very useful for others if you decide to package your code, you can instead easily create a function to emulate the same effect.
You can then call the function, assign it to another variable as required, or even nest them as required to assign default values down a hierarchy of variables.
EDIT:
As a side note, the function will also assign the default value to the variable even if it has not yet been defined.
You can create a similar function, or modify the function above to the following, if you would prefer to only update the variable if it is not defined.
很简单,php 是一个 php 预处理器,只需预处理包含运算符的 php 文件即可将所需的所有运算符转换为其底层表示形式。
否则可悲的是 php 不是 perl/lisp,所以你不能简单地在运行时将语言扩展到那个程度
its simple, php is a php preprocessor, simply preprocess your php file containing your operator to translate all operators you want into it's underlying representation.
otherwise sadly php is not perl/lisp so you can't simply extend the language at runtime to that extent
您可以使用函数:
输出:
或者如果您更喜欢分配:
输出:
You could use a function:
Output:
Or if you prefer assignment:
Output: