开发者

php global variables

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜