Posts Tagged ‘法数’

类的另类用法–数据的封装

星期一, 06月 2nd, 2008

类de另类用法–数据de封装
一般de情况下,如果使用classname::property是不能访问到类de属性de,但可以用classname::method()使用类de方法.同样de也不能用objectname->property访问到类de方法里de变量.利用这一特点,我可以将一些数据保存于类中,有点象c de私有属性.
<?
class data {
function value($var) {
static $d = array();
if(func_num_args() > 1) {
$d[$var] = func_get_arg(1);
}else {
return $d[$var];
}
}
}
//测试:
data::value("a",1);
data::value("b",2);
echo data::value("a");
echo data::value("b");
?>

Get或Post提交值的非法数据处理

星期一, 06月 2nd, 2008

Get或Post提交值de非法数据处理

<?php
//********************************************************
//– 程序名称:StrSwap V1.01
//– 程序编写:[email]cngift@163.com[/email]
//– 完成: 2002-8-1
//– 程序用途:Get或Post提交值de非法数据处理
//– 备注: 本程序需要加载在所有程序处理前使用,以便自动进行
//– 程序中使用de变量de替换
//– 由于发现严重BUG紧急升级
//– Copyright By cngift ◎ 2002
//********************************************************
class StrSwap{
//当以Get方式提交变量时用于连接变量de连接符
var $GetSplitStr = “&&”;
var $TempArray = array();
var $VariableArray = array();
//********************************************************
//– 程序名称:Main()
//– 程序用途:本类de默认运行方式
//– 传入参数:无
//********************************************************
function Main(){
global $REQUEST_METHOD;
if(”GET”==$REQUEST_METHOD){
$this->SubGetStrToArray();
}
if(”POST”==$REQUEST_METHOD){
$this->SubPostStrToArray();
}
$this->GlobalVariable();

}
//********************************************************
//– 程序名称:SubGetStrToArray()
//– 程序用途:当变量以Get方式提交时所调用de方法
//– 传入参数:无
//********************************************************
function SubGetStrToArray(){
global $QUERY_STRING;
$this->TempArray = explode($this->GetSplitStr,$QUERY_STRING);
for($i=0;$i<sizeof($this->TempArray);$i ){
$temp = explode(’=',$this->TempArray[$i]);
$this->VariableArray[$i][0] = $temp[0];
$this->VariableArray[$i][1] = $this->StrReplace($temp[1]);
}
}
//********************************************************
//– 程序名称:SubPostStrToArray()
//– 程序用途:当变量以POST方式提交时所调用de方法
//– 传入参数:无
//********************************************************
function SubPostStrToArray(){
global $_POST;
reset($_POST);
for($i=0;$i<count($_POST);$i ){
$this->VariableArray[$i][0] = key($_POST);
$this->VariableArray[$i][1] = $this->StrReplace($_POST[key($_POST)]);
next($_POST);
}
}
//********************************************************
//– 程序名称:StrReplace()
//– 程序用途:替换变量中de非法字符
//– 传入参数:变量值
//********************************************************
function StrReplace($str){
$str = StripSlashes($str);
$str = str_replace(chr(92),”,$str);
$str = str_replace(chr(47),”,$str);
$str = str_replace(chr(10).chr(13),”<br>”,$str);
$str = str_replace(’<’,”<”,$str);
$str = str_replace(’>’,”>”,$str);
$str = str_replace(’;',”;”,$str);
$str = str_replace(’”‘,”“”,$str);
$str = str_replace(”‘”,”‘”,$str);
$str = str_replace(” “,” “,$str);
$str = str_replace(”/**/”,” “,$str);
return trim($str);
}
//********************************************************
//– 程序名称:GlobalVariable()
//– 程序用途:声明变量为全局变量方便其他程序调用
//– 传入参数:无
//********************************************************
function GlobalVariable(){
for($i=0;$i<sizeof($this->VariableArray);$i ){
global $$this->VariableArray[$i][0];
${$this->VariableArray[$i][0]} = $this->VariableArray[$i][1];
}
}
}
?>