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?

1
  • add some more detail , statement above are not clear. Commented Sep 1, 2010 at 13:52

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.