欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂PHP培訓(xùn)簡述PHP單例模式模擬Java Bean實(shí)現(xiàn)方法

2019-04-03 15:35:42 2432瀏覽

本篇文章扣丁學(xué)堂PHP培訓(xùn)小編給喜歡PHP開發(fā)技術(shù)的讀者們分享一下PHP單例模式模擬Java Bean實(shí)現(xiàn)方法,文中有對PHP面向?qū)ο蟪绦蛟O(shè)計(jì)相關(guān)操作技巧的簡單介紹,對PHP開發(fā)技術(shù)感興趣的小伙伴隨小編來看一下吧。



扣丁學(xué)堂PHP培訓(xùn)簡述PHP單例模式模擬Java Bean實(shí)現(xiàn)方法



問題:根據(jù)如下楊輝三角形

PHP學(xué)習(xí)


實(shí)現(xiàn)一個(gè)get_value($row,$col)方法:


(前一個(gè)由于代碼是手機(jī)編輯的,很亂,重新發(fā)下)只是為了實(shí)現(xiàn)這個(gè)方法,很簡單,幾行代碼就能實(shí)現(xiàn),但如果行和列的值稍微大點(diǎn),你就發(fā)現(xiàn),運(yùn)行時(shí)間很長。所以就這次的題做了個(gè)稍微復(fù)雜點(diǎn)的例子,說明下單例模式的使用、static的使用、模擬Java Bean、static的使用、遞歸函數(shù)案例等。


/**
 * author Winter
 * 2016-11-22
 * PHP的單例模式
 * 模擬Java Bean
 * Class Php_bean
 */
class Php_bean{
  private static $_instance = null;
  private function __construct(){}
  private $hit = 0;//命中次數(shù)
  private $array = array();//緩存
  private $itratorCount = 0;//迭代次數(shù)
  public function add_itratorCount(){
    $this->itratorCount ++;
  }
  public function get_itratorCount(){
    return $this->itratorCount;
  }
  public function set_cache($row,$col,$value){
    $this->array[$row."_".$col] = $value;
  }
  public function get_cache($row,$col){
    if(isset($this->array[$row."_".$col])){
      return $this->array[$row."_".$col];
    }else{
      return false;
    }
  }
  public function add_hit(){
    $this->hit ++;
  }
  public function get_hit(){
    return $this->hit;
  }
  public static function instance(){
    if(self::$_instance instanceof self) return self::$_instance;
    self::$_instance = new self;
    return self::$_instance;
  }
}
/**
 * @param $row 行
 * @param $col 列
 * @return int
 */
function get_value($row,$col){
  $php_bean = Php_bean::instance();
  $php_bean->add_itratorCount();
  if($col > $row) return 0;
  if($row <=0) return 0;
  if($col == $row) return 1;
  if($row == 1) return 1;
  if($col == 1) return 1;
  $pre = $php_bean->get_cache($row-1,$col-1);
  $next = $php_bean->get_cache($row-1,$col-0);
  if($pre === false){
    $pre = get_value($row-1,$col-1);
    $php_bean->set_cache($row-1,$col-1,$pre);
  }else{
    $php_bean->add_hit();
  }
  if($next === false){
    $next = get_value($row-1,$col-0);
    $php_bean->set_cache($row-1,$col-0,$next);
  }else{
    $php_bean->add_hit();
  }
  $value = $pre + $next;
  return $value;
}
$v = get_value(6,6);
var_dump($v);
$php_bean_obj = Php_bean::instance();
echo "hit:".$php_bean_obj->get_hit()."<br/>";
echo "itratorCount:".$php_bean_obj->get_itratorCount()."<br/>";


運(yùn)行結(jié)果:


int(1) hit:0

itratorCount:1



想要了解更多關(guān)于PHP開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂PHP培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的PHP講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂PHP技術(shù)交流群:374332265。


                          


 【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】         【掃碼進(jìn)入HTML5進(jìn)階免費(fèi)公開課】



查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>

標(biāo)簽: PHP培訓(xùn) PHP基礎(chǔ)教程 PHP學(xué)習(xí)視頻 PHP教學(xué)視頻 PHP入門教程 PHP教程視頻 PHP在線學(xué)習(xí) PHP在線視頻 PHP在線教程 扣丁學(xué)堂PHP培訓(xùn)

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部