0

I need a PHP function with default argument value , I don't need string value for default argument .

I need call some else functions as default argument value

for example :

class main {

    public function get_Date(){

      date_default_timezone_set('America/New_York');
      return date('Y');

    }

   // My problem is here !

  public function display_Time ($time = $this->get_Date()){

    return $time ;
  }

}

I can't transfer get_Date() value to $time variable as default argument value.

please help ME.

3 Answers 3

1
class main {

    public function get_Date($tz = 'America/New_York'){

      date_default_timezone_set($tz);
      return date('Y');

    }

  public function display_Time ($time = null){

    return $time ? $time : $this->get_Date();
  }

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

Comments

1

That is the right call although it makes no sense

$time = $this->get_Date();

 public function display_Time ($time){
    return $time ;
  }

Comments

0

Workaround:

public function display_Time ($time = null){
  if (null === $time) $time = $this->get_Date();
  return $time ;
}

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.