PHP Static class instance code

Source: Internet
Author: User
In PHP, using the keyword static modifier member properties and member methods are called static properties and static methods. static properties and static methods do not need to be used directly in cases where the class is instantiated.

Static properties

Unlike normal member properties, a static property belongs to the class itself and not to any instance of the class. Static properties can be seen as global variables stored in classes that can be accessed from anywhere through classes.

Because static properties are not constrained by any specific object, you do not need to establish a class instance, but you can directly reference a static property in the class, using the syntax format as follows:


Class Name:: $ static property name


Where the symbol "::" is called the scope-resolution operator, which can be used to access static members, static methods and constants, and to override members and methods in a class.

If you want to access a static property in a member method of the same class, you can do so by adding the operator "Self::" Before the name of the static property.

Here is the PHP sample code

<?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 data source, parameter is an array.    </Description > Function binddata ($arr) {$this->datasource = $arr;    }///< Description >//Set 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 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 static files.    </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.