Home  >  Article  >  Backend Development  >  Use the ob function and file_put_contents in php to generate a simple static page class

Use the ob function and file_put_contents in php to generate a simple static page class

WBOY
WBOYOriginal
2016-08-08 09:25:441296browse
<?php

 class StaticHtml{
	private $htmlpath="data/html/";
	private $key;
	private $ismd5 =false;
	private $suffix=".html";

	public function start(){
		return ob_start();
	}

	public function end($key){
		$this->key=$key;
		$this->html();
		return ob_end_clean();
	}

	public function get($key){
		$filename =$this->getFilename($key);
		if(!$filename || !file_exists($filename)){return false;}
		include($filename);
		return true;
	}

   public function html(){
   		$filename =$this->getFilename($this->key);
        if(!$filename) {return  false;}
        return file_put_contents($filename,ob_get_contents());
   }

   public function getFilename($key){
   		$filename =($this->ismd5==true)?md5($key):$key;
   		if(!is_dir($this->htmlpath)){return false;}
   		return $this->htmlpath.'/'.$filename.$suffix;
   }

}

/**
 * 使用
 */
//实例化对象
$htmlObj = new StaticHtml();
//生成静态页面
$htmlObj->start();
$htmlObj->end(100);
//引用静态页面
$htmlObj->get(100);

?>

The above introduces the class that uses the ob function and file_put_contents in PHP to generate a simple static page, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn