简体   繁体   English

试图理解 openssl_sign 中的 &$signature 参数

[英]Trying to understand &$signature parameter in openssl_sign

I'm trying to understand -> https://www.php.net/manual/en/function.openssl-sign.php which has &$signature parameter.我试图理解-> https://www.php.net/manual/en/function.openssl-sign.php具有&$signature参数。 I read this -> PHP &$string - What does this mean?我读到这个 -> PHP &$string - 这是什么意思? but still don't understand why must we have &$signature as a parameter or how do we use it.但仍然不明白为什么我们必须有&$signature作为参数或者我们如何使用它。 The examples at https://www.php.net did not describe the use of that variable. https://www.php.net上的示例没有描述该变量的使用。

Here's one of their examples.这是他们的一个例子。

<?php
// $data is assumed to contain the data to be signed

// fetch private key from file and ready it
$pkeyid = openssl_pkey_get_private("file://src/openssl-0.9.6/demos/sign/key.pem");

// compute signature
openssl_sign($data, $signature, $pkeyid);

// free the key from memory
openssl_free_key($pkeyid);
?>

Btw, have a fabulous coding new year 2022!顺便说一句,2022 年新年编码很棒!

It means you're passing a variable 'by reference' ( https://www.php.net/manual/en/language.references.pass.php ), which, in the simplest terms, means that the variable you pass can be modified by the method or function it's passed to without having to return it.这意味着您通过引用传递一个变量( https://www.php.net/manual/en/language.references.pass.php ),用最简单的术语来说,这意味着您传递的变量可以由方法或 function 修改,无需返回它。

For example:例如:

$a = 1;

function foo($value) {
    $value++;
}

foo($a);

// $a is still 1
var_dump($a);

Whereas specifying the function's signature as &$value :而将函数的签名指定为&$value

$b = 1;

function bar(&$value) {
    $value++;
}

bar($b);

// $b is 2
var_dump($b);

So the openssl_sign() function is likely to be modifying the value of $signature without you having to request it's updated value via a getter.因此openssl_sign() function 可能会修改$signature的值,而无需您通过 getter 请求它的更新值。

It should also be noted that objects are always passed by reference, so do not need the &$ signature - their state will always be modified and can be used outside of the function they are passed to subsequently.还应注意,对象始终通过引用传递,因此不需要&$签名 - 它们的 state 将始终被修改,并且可以在随后传递给的 function 之外使用。

Function 'openssl_sign' is defined to computes a signature which is passed by reference. Function 'openssl_sign' 被定义为计算通过引用传递的签名。

Without '&', parameter signature won't change after the function is called.如果没有 '&',则在调用 function 后参数签名不会改变。

If you pass vars by reference to a function so the function can modify the variable directly without returning value.如果您通过引用 function 来传递 var,那么 function 可以直接修改变量而不返回值。

https://www.php.net/manual/en/language.references.pass.php https://www.php.net/manual/en/language.references.pass.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM