views:

38

answers:

2

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 function f(var=$config['val']){} will this assignment work?

+6  A: 

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.

Pekka
A: 

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();
Yorirou