开发者

php global variables

开发者 https://www.devze.com 2023-01-13 20:16 出处:网络
I have a global variable $config, now i have a class and i want to use a value from config as a default argument for a class method like f开发者_运维技巧unction f(var=$config[\'val\']){} will this ass

I have a global variable $config, now i have a class and i want to use a value from config as a default argument for a class method like f开发者_运维技巧unction f(var=$config['val']){} will this assignment work?


will this assignment work?

No, it won't.

There is no way to do this automatically in the function definition.

You would have to define an empty default:

function f($var = null) {  .... }

and then fill $var with a value from your configuration array inside the method if it is null.


No. What I would do is to add $config as an field to the class, like this:

class MyAwesomeClass {
  public $config;

  public function f() {
    ...
  }
}

$cls = new MyAwesomeClass;
$cls->config = $GLOBALS['config'];
$cls->f();
0

精彩评论

暂无评论...
验证码 换一张
取 消