1

I have a function like this:

protected function example($param = null)
{
}

and when I call it like this:

$this->example("string");

or like this: $string = "string"; $this->example("string");

the parameter value is still null. What am I doing wrong?

6
  • 1
    I'd suggest putting in a var_dump and tracing through your code to make sure it is doing what you expect it to and for sanity reasons. Make sure you're calling the right file as well. Commented Sep 22, 2015 at 21:22
  • Can you show a full example that shows this issue? Are you sure you are calling that function? This looks to be in a class (because of the protected keyword), so you'd need $this->example("string");. Commented Sep 22, 2015 at 21:23
  • 2
    example() is not the same as $this->example() or $class->example() in OOP Commented Sep 22, 2015 at 21:24
  • How are you determining that $param is null? Commented Sep 22, 2015 at 21:39
  • Newbi3, I am actually calling the function from inside the same class. I have had it write the $param value to a file, and the value is always whatever I set the default to, never the value I pass in. Commented Sep 22, 2015 at 21:40

2 Answers 2

1

Try returning the variable.

protected function example($param = null)
{
    return $param;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Having the parameter set to NULL only assigns the variable $param to NULL when no parameter is passed.

like when you call it like this $this->example();

Otherwise $param will be set to whatever you pass it.

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.