Skip to content

Commit

Permalink
使用单例模式取得JSON信息
Browse files Browse the repository at this point in the history
  • Loading branch information
leinue committed Sep 22, 2014
1 parent c5c5b46 commit bf473be
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Binary file added beta/favicon.ico
Binary file not shown.
65 changes: 65 additions & 0 deletions beta/fun/cls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

class infoMgr{

public static $handle;
static private $_instance=NULL;

private function __construct(){}
private function __clone(){}

static function getInstance(){
if(self::$_instance==NULL){
self::$_instance=new infoMgr();
}
return self::$_instance;
}

protected function read(){
do{
$data = fread(self::$handle, 1024);
if (strlen($data) == 0) {break;}
$contents .= $data;
}while(true);

return $contents;
}
}

class decodeJSON{
protected $contents;
function __construct($contents){$this->contents=$contents;}

function decode(){return json_decode($this->contents);}
}

class todayMgr extends infoMgr{
protected $infomgr;

function __construct(infoMgr $infoMgr){$this->infomgr=$infoMgr;}

function getJSON($url=NULL){
parent::$handle=fopen("http://news-at.zhihu.com/api/3/news/latest","rb");
if(parent::$handle){
return $this->infomgr->read();
}else{return false;}
}

function getContext(decodeJSON $DJSON){
$jsonData=$DJSON->decode();
return $jsonData;
}
}

class beforeMgr extends infoMgr{

}


$im=infoMgr::getInstance();
$tm=new todayMgr($im);
//echo $tm->getJSON();

print_r($tm->getContext(new decodeJSON($tm->getJSON())));

?>

0 comments on commit bf473be

Please sign in to comment.