如何在 PHP 中创建新运算符?

发布于 2024-11-15 13:17:22 字数 355 浏览 1 评论 0原文

我看到自己在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

水波映月 2024-11-22 13:17:22

如果不更改 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

坏尐絯 2024-11-22 13:17:22

在 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.

装迷糊 2024-11-22 13:17:22

你不能在 PHP 中执行此操作,不能使用此语法。

<?
$test = array("3 is 5", "3 is not 5"); 
$r = $teste [+( 3 != 5)];
echo $r; //return "3 is not 5" 
?>

You cannot do this in PHP,not with this syntax.

<?
$test = array("3 is 5", "3 is not 5"); 
$r = $teste [+( 3 != 5)];
echo $r; //return "3 is not 5" 
?>
街角迷惘 2024-11-22 13:17:22

当想知道是否可以创建新操作时,这似乎是最常出现的一个示例。我个人认为 ?= 会非常方便。

由于创建新运算符将涉及创建您自己的 PHP 版本,并且如果您决定打包代码,对其他人来说可能不是很有用,因此您可以轻松创建一个函数来模拟相同的效果。

/**
 * Global function to simplify the process of
 * checking to see if a value equates to true
 * and setting it to a new value if not.
 * 
 * @param mixed $var the original variable to
 * check and see if it equates to true/false
 * @param mixed $default the default value to
 * replace the variable if it equates to false
 * 
 * @return mixed returns the variable for
 * either further processing or assignment.
 */
function _d( &$var, $default ) {
    return $var = $var ?: $default;
}

然后,您可以调用该函数,根据需要将其分配给另一个变量,甚至根据需要嵌套它们以在变量层次结构中分配默认值。

_d( $variable, 'default value' );
// $variable = $variable ?: 'default value';

$variable1 = _d( $variable, 'default value' ) . 's';
// $variable1 = ( $variable = $variable ?: 'default value' ) . 's';

_d( $variable, _d( $variable1, 'default value' ) );
// $variable = $variable ?: ( $variable1 = $variable1 ?: 'default value' );

编辑:

作为旁注,该函数还将为变量分配默认值,即使该变量尚未定义。

如果您希望仅更新未定义的变量,则可以创建类似的函数,或将上面的函数修改为以下函数。

return $var = isset( $var ) ? $var : $default;}

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.

/**
 * Global function to simplify the process of
 * checking to see if a value equates to true
 * and setting it to a new value if not.
 * 
 * @param mixed $var the original variable to
 * check and see if it equates to true/false
 * @param mixed $default the default value to
 * replace the variable if it equates to false
 * 
 * @return mixed returns the variable for
 * either further processing or assignment.
 */
function _d( &$var, $default ) {
    return $var = $var ?: $default;
}

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.

_d( $variable, 'default value' );
// $variable = $variable ?: 'default value';

$variable1 = _d( $variable, 'default value' ) . 's';
// $variable1 = ( $variable = $variable ?: 'default value' ) . 's';

_d( $variable, _d( $variable1, 'default value' ) );
// $variable = $variable ?: ( $variable1 = $variable1 ?: 'default value' );

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.

return $var = isset( $var ) ? $var : $default;}
九局 2024-11-22 13:17:22

很简单,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

李不 2024-11-22 13:17:22

您可以使用函数:

<?php
    function defvar(&$var) {
        if (empty($var)) { $var = 'default value'; }
    }

    // testing
    $set = 'test';
    defvar($set);
    echo $set;

    defvar($notset);
    echo $notset;
?>

输出:

test
default value

或者如果您更喜欢分配:

<?php
    function setdefvar($var) {
        if (empty($var)) { return 'default value'; }
        else { return $var; }
    }

    // testing
    $set = 'test';
    $set = setdefvar($set);
    echo $set;

    $notset = setdefvar($notset);
    echo $notset;
?>

输出:

test
default value

You could use a function:

<?php
    function defvar(&$var) {
        if (empty($var)) { $var = 'default value'; }
    }

    // testing
    $set = 'test';
    defvar($set);
    echo $set;

    defvar($notset);
    echo $notset;
?>

Output:

test
default value

Or if you prefer assignment:

<?php
    function setdefvar($var) {
        if (empty($var)) { return 'default value'; }
        else { return $var; }
    }

    // testing
    $set = 'test';
    $set = setdefvar($set);
    echo $set;

    $notset = setdefvar($notset);
    echo $notset;
?>

Output:

test
default value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文