简体   繁体   English

PHP将变量传递给函数的返回值

[英]PHP pass variable to the function's return

Is it possible to do something like this: 是否可以做这样的事情:

Class::function('some_text') = 'aaaaa';

And get this 'aaaaa' string inside the Class::function() ? 并在Class :: function()中获取此“ aaaaa”字符串?

Yes it is by doing this: 是的,它是通过执行以下操作:

MyClass::func('some_text', 'aaaaa');

Eg 例如

class MyClass {
    public static function func($text, $aaa) {
        ...
    }
}

Alternatively (and much worse then the previous IMHO): 或者(比以前的恕我直言更糟):

global $foo;
$foo = 'bar';

class Baz {
    public static function bong() {
        global $foo;
        ...
    }
}

That syntax is not available in PHP. 该语法在PHP中不可用。

In Perl, this type of function would be called an LVALUE function . 在Perl中,这种类型的函数称为LVALUE function

With your case it seems like you're searching for a solution like this? 对于您的情况,似乎您正在寻找这样的解决方案?

Session::Set(array("key" => "value"));

class Session {
    public static function Set($kvp) {
        foreach ($kvp as $key => $value) {
            echo $key . " is " . $value . "<br />";
        }
    }
}

you can return a reference and change the value of it. 您可以返回参考并更改其值。

class MyClass {

  private static $variable;

  public static function &func($random_param) {
    return self::$variable;
  }
}

call it like this 这样称呼它

$reference = &MyClass::func('asd');
$reference = "15";

echo MyClass::func('asdasd'); // "15"

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

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