簡體   English   中英

PHP默認參數值

[英]PHP Default Argument Value

我需要一個帶有默認參數值的PHP函數,我不需要字符串值作為默認參數。

我需要調用其他函數作為默認參數值

例如 :

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 ;
  }

}

我無法將get_Date()值傳輸到$time變量作為默認參數值。

請幫我。

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

}

這是正確的選擇,盡管沒有任何意義

$time = $this->get_Date();

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

解決方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM