3.PHP静态类

简介:
Java代码   收藏代码
  1. <?php  
  2. /******************8PHP静态类*************/  
  3. class Shtml  
  4. {  
  5.     var $Templet;  
  6.     var $DataSource;  
  7.     var $Dir;  
  8.   
  9.     var $fileName;  
  10.     var $mod;  
  11.     var $handle;  
  12.   
  13.     function Shtml($fileName = "")  
  14.     {  
  15.         $this->fileName = $fileName;  
  16.         $this->mod = "wb";  
  17.         $this->handle = false;  
  18.   
  19.         $this->Templet = "";  
  20.         $this->DataSource = array();  
  21.         $this->Dir = "";  
  22.     }  
  23.   
  24.     // 绑定数据源,参数为一数组  
  25.     function BindData($arr)  
  26.     {  
  27.         $this->DataSource = $arr;  
  28.     }  
  29.   
  30.     // 设置文件存放路径  
  31.     function SetDir($dir)  
  32.     {  
  33.         $this->Dir = $dir;  
  34.     }  
  35.     function SetFileName($fileName)  
  36.     {  
  37.         return $this->fileName = $fileName;  
  38.     }  
  39.   
  40.     function GetMod()  
  41.     {  
  42.         return $this->mod;  
  43.     }  
  44.     function SetMod($mod)  
  45.     {  
  46.         return $this->mod = $mod;  
  47.     }  
  48.     function Open()  
  49.     {  
  50.         if (substr($this->fileName, 01) == "/")  
  51.         $this->fileName = $_SERVER['DOCUMENT_ROOT'] . $this->fileName;  
  52.         if ($this->handle = fopen($this->fileName, $this->mod))  
  53.         return $this->handle;  
  54.         else  
  55.         return false;  
  56.     }  
  57.     function Close()  
  58.     {  
  59.         return fclose($this->handle);  
  60.     }  
  61.     function Write($content)  
  62.     {  
  63.         return fwrite($this->handle, $content);  
  64.     }  
  65.     function MkDir($pathname)  
  66.     {  
  67.         $currentPath = "";  
  68.         str_replace("\\", "/", $pathname);  
  69.         $pathArr = split("/", $pathname);  
  70.         if ($pathArr[0] == "") { //使用绝对路径  
  71.             $currentPath = $_SERVER['DOCUMENT_ROOT'];  
  72.         } else {  
  73.             $currentPath = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['PHP_SELF']);  
  74.         }  
  75.         for ($i = 0; $i < count($pathArr); $i++) {  
  76.             if ($pathArr[$i] == "")  
  77.             continue;  
  78.             else  
  79.             if (is_dir($currentPath . " / " . $pathArr[$i]))  
  80.             $currentPath = $currentPath . " / " . $pathArr[$i];  
  81.             else  
  82.             mkdir($currentPath = $currentPath . " / " . $pathArr[$i]);  
  83.         }  
  84.     }  
  85.   
  86.     // 生成静态文件  
  87.     function Create()  
  88.     {  
  89.         $tmp = $this->Templet;  
  90.         foreach ($this->DataSource as $key => $value) {  
  91.             $tmp = str_replace(" < FIELD_" . $key . " > ", $value, $tmp);  
  92.         }  
  93.         $this->MkDir(dirname($this->fileName));  
  94.         $this->Open();  
  95.         $this->Write($tmp);  
  96.         $this->Close();  
  97.     }  
  98. }  
  99.   
  100. function CreateShtml() {  
  101.     ob_start("callback_CteateShtml");  
  102. }  
  103.   
  104. function callback_CteateShtml($buffer) {  
  105.     $page = intval(@$_REQUEST["page"]);  
  106.     $shtml = new Shtml();  
  107.     $shtml->SetFileName($_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['PHP_SELF']) . "/" . basename($_SERVER['PHP_SELF'], ".php") . ($page == 0 ? "" : "_" . strval($page)) . ".html");  
  108.     $shtml->Templet = $buffer;  
  109.     $shtml->Create();  
  110.     return $buffer;  
  111. }  
  112. ?>  

把上面的代码保存为 static.php
执行下面页面 phpfans.php

Java代码   收藏代码
  1. <?php  
  2. include("static.php");  
  3. CreateShtml();  
  4. ?>  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  8. <title>无标题文档</title>  
  9. </head>  
  10. <body>  
  11. 撒旦法第三方  
  12. </body>  
  13. </html>  

将会生成一个 phpfans.html的静态页面,如果有参数,则用page来传递 如 phpfans.php?page=1则生成 phpfans_1.html的静态页面

相关文章
|
PHP 开发者
《阿里云栖开发者沙龙PHP技术专场-静态扫描为你的项目上线保驾护航-周梦康》电子版地址
阿里云栖开发者沙龙PHP技术专场-静态扫描为你的项目上线保驾护航-周梦康
164 0
《阿里云栖开发者沙龙PHP技术专场-静态扫描为你的项目上线保驾护航-周梦康》电子版地址
|
开发框架 前端开发 Java
PHP 的基础语法_动态网站和静态网站| 学习笔记
快速学习 PHP 的基础语法_动态网站和静态网站。
111 0
PHP 的基础语法_动态网站和静态网站| 学习笔记
|
缓存 PHP
PHP 静态缓存
php静态缓存
|
测试技术 PHP 开发者
阿里云栖开发者沙龙PHP技术专场-静态扫描为你的PHP项目上线保驾护航
摘要:在4月20日的阿里云栖开发者沙龙PHP技术专场上,阿里云资深研发工程师周梦康(楚松)为大家分享了如何借助静态扫描来为我们的PHP项目上线保驾护航,他从Why、How和What三个角度为大家分享了静态扫描的落地实践和最终成果。
1787 0
|
IDE 测试技术 Shell
使用 Phan 为你的 PHP 项目保驾护航 - 代码静态扫描
原文:我的个人博客 https://mengkang.net/1356.html 很多时候,最大的优势在某些情况下就会变成最大的劣势。PHP 语法非常灵活,也不用编译。但是在项目比较复杂的时候,可能会导致一些意想不到的 bug。
2093 0
|
PHP 数据安全/隐私保护 对象存储
oss php sdk+laravel搭建图片处理静态网站
结合oss的图片处理功能介绍如何利用oss搭建一个低成本高性能的静态服务器
8968 0