PHP static class

Source: Internet
Author: User

Copy codeThe Code is as follows: <? Php
Class Shtml
{
Var $ Templet;
Var $ DataSource;
Var $ Dir;

Var $ fileName;
Var $ mod;
Var $ handle;

Function Shtml ($ fileName = "")
{
$ This-> fileName = $ fileName;
$ This-> mod = "wb ";
$ This-> handle = false;

$ This-> Templet = "";
$ This-> DataSource = array ();
$ This-> Dir = "";
}

/// <Description>
/// Bind the data source. The parameter is an array.
/// </Description>
Function BindData ($ arr)
{
$ This-> DataSource = $ arr;
}

/// <Description>
/// Set the file storage path.
/// </Description>
Function SetDir ($ dir)
{
$ This-> Dir = $ dir;
}
Function SetFileName ($ fileName)
{
Return $ this-> fileName = $ fileName;
}

Function GetMod ()
{
Return $ this-> mod;
}
Function SetMod ($ mod)
{
Return $ this-> mod = $ mod;
}
Function Open ()
{
If (substr ($ this-> fileName, 0, 1) = "/")
$ This-> fileName = $ _ SERVER ['document _ root']. $ this-> fileName;
If ($ this-> handle = fopen ($ this-> fileName, $ this-> mod ))
Return $ this-> handle;
Else
Return false;
}
Function Close ()
{
Return fclose ($ this-> handle );
}
Function Write ($ content)
{
Return fwrite ($ this-> handle, $ content );
}
Function MkDir ($ pathname)
{
$ CurrentPath = "";
Str_replace ("\", "/", $ pathname );
$ PathArr = split ("/", $ pathname );
If ($ pathArr [0] = "") // use the absolute path
{
$ CurrentPath = $ _ SERVER ['document _ root'];
}
Else
{
$ CurrentPath = $ _ SERVER ['document _ root']. dirname ($ _ SERVER ['php _ SELF ']);
}
For ($ I = 0; $ I <count ($ pathArr); $ I ++)
{
If ($ pathArr [$ I] = "")
Continue;
Else
If (is_dir ($ currentPath. "/". $ pathArr [$ I])
$ CurrentPath = $ currentPath. "/". $ pathArr [$ I];
Else
Mkdir ($ currentPath = $ currentPath. "/". $ pathArr [$ I]);
}
}

/// <Description>
/// Generate a static file.
/// </Description>
Function Create ()
{
$ Tmp = $ this-> Templet;
Foreach ($ this-> DataSource as $ key => $ value)
{
$ Tmp = str_replace ("<FIELD _". $ key. ">", $ value, $ tmp );
}
$ This-> MkDir (dirname ($ this-> fileName ));
$ This-> Open ();
$ This-> Write ($ tmp );
$ This-> Close ();
}
}

Function CreateShtml ()
{
Ob_start ("callback_CteateShtml ");
}
Function callback_CteateShtml ($ buffer)
{
$ Page = intval (@ $ _ REQUEST ["page"]);
$ Shtml = new Shtml ();
$ Shtml-> SetFileName ($ _ SERVER ['document _ root']. dirname ($ _ SERVER ['php _ SELF ']). "/". basename ($ _ SERVER ['php _ SELF '], ". php "). ($ page = 0? "": "_". Strval ($ page). ". htm ");
$ Shtml-> Templet = $ buffer;
$ Shtml-> Create ();
Return $ buffer;
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.